You are on page 1of 3

ASSIGNMENT NO -22 PROBLEM STATEMENT:

Implementation of a lex and yacc program to evaluate postfix expression .

THEORY:
INTRODUCTION: Lex generates C code for a lexical analyzer, or scanner. It uses patterns that match strings in the input and converts the strings to tokens. Tokens are numerical representations of strings, and simplify processing. This is illustrated in Figure 1-1. As lex finds identifiers in the input stream, it enters them in a symbol table. The symbol table may also contain other information such as data type (integer or real) and location of the variable in memory. All subsequent references to identifiers refer to the appropriate symbol table index. Yacc generates C code for a syntax analyzer, or parser. Yacc uses grammar rules that allow it to analyze tokens from lex and create a syntax tree. A syntax tree imposes a hierarchical structure on tokens. For example, operator precedence and associativity are apparent in the syntax tree. The next step, code generation, does a depth-first walk of the syntax tree to generate code.

First, we need to specify all pattern matching rules for lex (bas.l) and grammar rules for yacc (bas.y). Commands to create our compiler, bas.exe, are listed below: yacc d bas.y # create y.tab.h, y.tab.c lex bas.l # create lex.yy.c cc lex.yy.c y.tab.c # compile/link lm

Yacc reads the grammar descriptions in bas.y and generates a parser, function yyparse, in file y.tab.c. Included in file bas.y are token declarations. These are
1

pattern descriptions in bas.l, includes file y.tab.h, and generates a lexical analyzer, function yylex, in file lex.yy.c. Finally, the lexer and parser are compiled and linked together to form the executable, bas.exe. From main, we call yyparse to run the compiler. Function yyparse automatically calls yylex to obtain each token.

%union: It defines the Stack type for the Parser. It is a union of various datas/structures/ objects. %token: These are the terminals returned by the yylex function to the yacc. A type of a token can be specified as %token <stack member> tokenName. %left: Specifies the left associativity of a Terminal Symbol. %right: Specifies the right associativity of a Terminal Symbol The basic idea of conversion from postfix to infix is as follows: 1. The postfix expression is scanned from left to right. 2. If a token is a number, the token is pushed onto the stack. 3. If a token is a operator, the top two infix subexpressions are popped from the stack and a new infix expression is constructed by combining these subexpressions with the operator in a normal infix manner. 4. The resulting expression is then pushed onto the stack. 5. Initially, we will place each subexpression in a parantheses to ensure the proper order of evaluation in the final expression. Example: consider the expression 34*12+TOKEN 3 4 * 1 3*4 2 3*4 1+2 3*4 (1+2)-(3*4) 2 1 3*4 1 STACK 3 4 3

+ -

ALGORITHM:
1.Start 2.Give input file (posteval.txt) to standard input function of lex program.
2

3.To recognize the token give it to yylex function. 4.Declare the associativity of operators . 5.In rule section ,define the patterns for various operators. 6.End of file through yywrap() function.

CONCLUSION:-T
Hence we have studied LEX and YACC program to evaluate postfix expression .

You might also like