You are on page 1of 5


The parser obtains a string of tokens from the lexical analyzer, as shown in Fig 4.1, and verifies that the string can be generated by the grammar for the source language. We expect the parser to report any syntax errors in an intelligible fashion. It should then also recover from commonly occurring errors so that it can continue parsing the remainder of the input program.

Source Program (C File)

Lexical Analyzer
Get Next Token


Parse Rest of Intermediate Tree Front end Representation

Symbol Table
Figure 4.1 Position of Parser in Compiler Model

Yacc, Occs are some of the tools which generates the codes for parsing. Here, we have used YACC (Yet Another Compiler–Compiler) to generate parser. Features of YACC (1) (2) It uses the Look Ahead LR (LALR) parsing technique to create a parser. It stores the action and goto table in the form of compressed array.

YACC Specifications A Yacc program consists of three parts: declarations %% production (translation) rules %% supporting C–routine

The translation rules part: In the part of the Yacc specification after the first %% pair. we put ordinary C we put the translation rules. Also in the declarations part are declarations of the grammar tokens. In the first section.exe (Parser) Figure 4.c a.SYNTAX ANALYZER Yacc Specifications 13 yacc. Tokens declared in this section can be used in the second and third sections of the Yacc specification..y y. Each rule consists of a grammar production and the associated semantic Source Program (C File) lex. A set of productions that we have been writing <left side> → <alt 1> | <alt 2> | … | <alt n> would be written in Yacc as <left side> : | . Here we place declarations of any temporaries used by the translation rules or procedures of the second and third sections. | . delimited by %{ and %}. <alt 1> <alt 2> <alt n> { semantic action 1 } { semantic action 2 } { semantic action n } .c Yacc C Compiler Symbol Table y.2 Creating the Parser with Yacc The declarations part: There are two optional sections in the declarations part of a Yacc program.yy.exe (Lexical Analyzer) Token Get Next Token Error Handler yyout..

we can assign precedences and associativities to terminals. The first left side is taken to be the start symbol. Using Yacc with Ambiguous Grammars When ambiguous grammar is used in the Yacc specification. Yacc provides a general mechanism for resolving shift/reduce conflicts. In a semantic action . so normally the semantic action computes a value for $$ in terms of the $i’s. the symbol $$ refers to the attribute value associated with the nonterminal on the left. Unless otherwise instructed Yacc will resolve all parsing action conflicts using the following two rules: 1) 2) A reduce/reduce conflict is resolved by choosing the conflicting production listed first in the Yacc specification. Alternative right side can be separated by a vertical bar. The supporting C–routines part: The third part of a Yacc specification consists of supporting C–routines. A lexical analyzer by the name yylex() must be provided. The declaration %left ‘+’ ‘-‘ . and a semicolon follows each left side with its alternatives and their semantic actions. and unquoted strings of letters and digits not declared to be tokens are taken to be nonterminals. Other procedures such as error recovery routines may be added as necessary. Since these default rules may not always be what the compiler designer wants.SYNTAX ANALYZER Yacc Specifications 14 In a Yacc production. A Yacc semantic action is a sequence of C statements. A shift/reduce conflict is resolved in favour of shift. Yacc will report number of parsing action conflicts that are generated. the LALR algorithm will generate parsing action conflicts. The semantic action is performed whenever we reduce by the associated production. while $i refers to the value associated with the ith grammar symbol (terminal or nonterminal) on the right. In the declarations portion. a quoted single character ‘c’ is taken to be the terminal symbol c. This rule resolves the shift/reduce conflict arising from the dangling–else ambiguity correctly.

Error Recovery in Yacc In Yacc. Typical choices are some subset of the nonterminals generating expressions. blocks and procedures. where the rightmost terminal does not supply the proper precedence to a production. When α is . as though it saw the token error on its input. Yacc pops symbol from its stack until it finds the topmost state on its stack whose underlying set of items includes an item of the form A → . SYNTAX ANALYZER Error Recovery in Yacc 15 We can force an operator to be a nonassociative binary operator (i. error α. In some situations. error is a Yacc reserved word. However. where A is a major nonterminal and α is a string of grammar symbols.. Yacc does not report shift/reduce conflicts that are resolved using the precedence and associativity mechanism. First. which presumably is defined in the declaration section. The parser then “shifts” a fictitious token error onto the stack. a reduction to A occurs immediately and the semantic action associated with the production A → error (which might be a user–specified . Similarly for an operator to be right associative right is used instead of left in the statement above. the designer decides what “major” nonterminals will have error recovery associated with them. when the parser generated by Yacc encounters an error. it treats the states whose sets of items contain error productions in a special way. Yacc will generate a parser from such a specification. two occurrences of the operator cannot be combined at all) by stating %nonassoc ‘<’ Moreover. perhaps the empty string. error recovery can be performed using a form of error productions. lowest firat.makes + and – be of the same precedence and be left associative.e. The designer then adds to the grammar error productions of the for A → error α. the tokens are given precedences in th order in which they appear in the declarations part. treating the error productions as ordinary productions. statements. we can force a precedence by appending to a production the tag %prec <terminal> The precedence and associativity of the production will then be the same as that of the terminal. On encountering an error. Tokens in the same declarations have the same precedence.

and “reduces” them by shifting them onto the stack. At this point. SYNTAX ANALYZER SYNTAX ANALYZER Error Recovery in Yacc Error Recovery in Yacc 15 16 If α is not empty. the parser will have error α on top of its stack. then it looks for this string of terminals on the input. . Yacc skips ahead on the input looking for a substring that can be reduced to α. The parser will then reduce error α to A. If α consists entirely of terminals. and resume normal parsing. The parser then discards input symbols until it finds an input symbol on which normal parsing can proceed.error–recovery routine) is invoked.