“Grammar is used throughout the parser to organize compiler front ends. A grammar naturally describes the hierarchical structure of most programming language constructs such as the Pascal Language. For example, an if- else statement in Pascal language can have the form If (expression) statement else statement. That is, an if-else statement is the concatenation of the keyword if , an opening parenthesis, an expression, a closing parenthesis, a statement, the keyword else, and another statement. Using the variable expr to denote an expression and variable stmt to denote a statement, this structuring rule can be expressed as: stmt if (expr) stmt else stmt in which the arrow may be read as “ can have the form” Such a rule is called a production. In production, lexical elements like the keyword if and the parentheses are called terminals. Variables like expr and stmt represent sequence of terminals and called non-terminals” [6]. Parser (Syntax Analysis) Design Parsing is the process of determining if a string of tokens can be generated by a grammar. To parse Pascal, it is sufficient to make a single left to right scan over the input, looking ahead one token at a time. Top-Down parsing constructs the nodes of a parse tree starting at the root and proceeding towards the leaves such as the simple example in Figure 8. To construct the parse tree, start at the root and repeatedly do the following two steps:
For” Statement grammar: <for statement> ::= for <variable identifier > ::= <expression> to <expression> do < statement> “Case” Statement grammar: <Case> := Case id Of <case_element> End ‘;’ | empty <case_element> := ‘’’ <case_label_list> ‘:’ <statement>’;’ <statement> <case_element> | < case_Label_list> := < Constant> ‘{‘ <case_label_list> | ‘,’ <constant> <case_label_list> |’{‘ <constant> := ‘’’ | ’+’ | ’-‘ | id | num “ Array” structure grammar: <OnDimArray> := array [ num .. num ] of <standard_type> <standard_type> := integer | real If logic statement grammar: <IFLogic> := <ANDLOGIC> Or < expression_list> <IFLogic> | empty <ANDLOGIC> := < expression_list> And <expression_list> <ANDLOGIC> | <expression_list> := < expression> | ‘,’ < expression > <expression> :=…….
Design Common Syntax Errors are several types of error, with consequences ranging from deficiencies in the formatting of the output to the calculation of wrong results. A compilation error (which prevents the compiler from compiling the source code) is usually a syntax error but could be an error in the compiler itself. A syntax error results when the source code does not obey the rules of the language. The compiler generates error messages to help the programmer to fix the code. The source code may compile to machine code which then fails upon execution. A run-time error causes this situation. Potentially the most serious type of error occurs when the program appears to be working but is performing faulty processing due to logic errors in the source code. I classify the common errors as syntax errors. I designed some of errors which the DCSPM compiler recognizes so, every token has ascii code such as ‘(‘ , ‘)’, …..so on. The token code in the symbol table represents its keyword such as: ‘program’ keyword has 323, ‘do’ keyword has 305, … so on. The method in DCSPM compiler looks like: static void t) { if (lookahead.code == t) { lookahead = nextToken(); } else switch (t) { case 40: Err += "\n" + "\n" + "Expected Missing '('"; break; case 41: Err += "\n" + "\n" + "Expected Missing ')'"; break; case 44: Err += "\n" + "\n" + "Expected Missing ','";
{ ; } <variable declaration> ::= <identifier { , <identifier> } : <type> <type> ::= <simple type> <simple type> ::= <type identifier> <type identifier> ::= <identifier> <statement part> ::= <compound statement>
<for statement> ::= for <variable identifier > ::= <expression> to <expression> do < statement> <expression> expression> | <simple expression> <relational operator> <simple expression> <simple expression> ::= <sign> <term> { <adding operator> <term> } <term> ::= <factor> { <multiplying operator> <factor> } <factor> ::= <variable> | ( <expression> ) <relational operator> ::= = | <> | < | <= | >= | > <adding operator> ::= + | - <multiplying operator> ::= * | / <variable> ::= <entire variable> <entire variable> ::= <variable identifier> <variable identifier> ::= <identifier> <identifier> ::= <letter> { <letter or digit> }