#include "stdafx.h" #include "example.h" #include "inp.h" #include "lex.h" #include "out.h" #include using namespace std; namespace experiment { void PutStd (string s) { PutStr (const_cast (s.c_str ())); } void PutLn (string s) { PutStd (s); PutEol (); } string GetIdent () { string result = IdentVal; CheckSymbol (IDENT); return result; } string GetNumber () { string result = NumVal; CheckSymbol (NUM); return result; } string GetFloat () { string result = NumVal; CheckSymbol (FLT); return result; } string GetString () { string result = StrVal; CheckSymbol (STR); return result; } string GetChar () { string result = string (1, ChrVal); CheckSymbol (CHR); return result; } void mainExpression (); void simpleExpression () { string value; if (sy == LPAR) { NextSymbol (); PutStd ("("); mainExpression (); PutStd (")"); CheckSymbol (RPAR); } else { if (sy == IDENT) value = GetIdent (); else if (sy == NUM) value = GetNumber (); else if (sy == FLT) value = GetFloat (); else if (sy == STR) value = GetString (); else if (sy == CHR) value = GetChar (); else Error ("Expression expected"); // inp.h PutStd (value); } } void mulExpression () { simpleExpression (); while (sy == ASTERISK || sy == SLASH) { if (sy == ASTERISK) PutStd ("*"); else PutStd ("/"); NextSymbol (); simpleExpression (); } } void addExpression () { mulExpression (); while (sy == PLUS || sy == MINUS) { if (sy == PLUS) PutStd ("+"); else PutStd ("-"); NextSymbol (); mulExpression (); } } void mainExpression () { PutStd ("["); addExpression (); PutStd ("]"); } void expression () { PutStd ("expression "); mainExpression (); PutEol (); } /* void addExpression1 () { simpleExpression (); if (sy == PLUS || sy == MINUS) { NextSymbol (); addExpression1 (); } } */ /* void addExpression2 () { addExpression2 (); if (sy == PLUS || sy == MINUS) { NextSymbol (); simpleExpression (); } } */ void statement (); void ifStatement () { PutLn ("if statement"); Indent (); CheckSymbol (IF_); CheckSymbol (LPAR); expression (); CheckSymbol (RPAR); PutLn ("then statement"); Indent (); statement (); Unindent (); if (sy == ELSE_) { PutLn ("else statement"); CheckSymbol (ELSE_); Indent (); statement (); Unindent (); } Unindent (); PutLn ("end of if statement"); } void compoundStatement () { CheckSymbol (LBRACE); PutLn ("compound statement"); Indent (); while (sy != RBRACE) { statement (); } Unindent (); PutLn ("end of compound statement"); CheckSymbol (RBRACE); } void simpleStatement () { PutLn ("simple statement"); expression (); CheckSymbol (ASSIGN); expression (); CheckSymbol (SEMICOLON); } void statement () { if (sy == LBRACE) compoundStatement (); else if (sy == IF_) ifStatement (); else simpleStatement (); } void example () { while (sy!=EOS) { statement (); } } } // end of namespace