Implementation of parsing process for Infix to Postfix Expression.
Problem Statement : Implementation of parsing process for
Infix to Postfix Expression.
Theory: Parsing Process:
It is also known as Syntax Analysis. It refers to analyze the input
require in order determine it’s graphical structure with respect to
given Formal Grammar.
Parsing process transforms the given input into tree data structure,
which is suitable for later processing and which computes implied
hierarchy at input generally parse tree from those tokens.
Structure of Parser Process life:
The files are divided into 3 sections, separated by lines that
contains signs as follows,
Definition section
%%
Rule section
%%
C code Section
Definition Section:
It is the place to define macros and to import header files written
in C.
It is also possible to write any C code here which will be copied
into generated source file.
Rule Section:
It is most important section. It is associates and patterns, which
statement patterns are simple. Rule Section later checks some text in
input matching to given pattern it executes associated c code.
C code Section:
This code section contains C statements and functions that are copied
to generate source file.
These statement contain code called by return in rule section.
Action:
The action part of rule consist of (b/k) beginning with ‘{’
ending with ‘}’.
yy parse();
main() repeatedly calls yyparse() in which later input files run out.
Example of parse process file
/File-(*Definition Section*)
%{
#include<stdio.h>
#include<tab.h>??
}%
%%
%{id}{yy?? =*yytext:retun id;}
{return *yytext}
%%yylex
%}
% token id
%%E:E+F{strcat/Estring,T String};
Strcat (Estrring, “+”);
Printf(“%s”, Estring);}
%% int main(void)
{
yyparse();
yyerror();
}
}
}
yyal??:
Whenever lex return token to parser the token has an associated
value. Lexer must store value in yyal refer
returning
yytab.h:
yycc defines token names in the parser as C processor names in
y.tab.h. So the lexer can use.
ytab.c:
yycc token use grammar and generate y.tab.c, the C language parser.
Abc.l
%{
#include”y.tab.h”
int yyal
%}
Id[a2 A2]
%%
{id}{yyal= yytext; return id}
{return *yytext}
%%
abc.y
%{
Char Estring[20];
Char Tstring[20];
Char Fstring[1];
%}
%token id
E:E+F{stat (E,F); strcat(I, “+”);};
;LT{strcpy(Estring,Tstring);}
T:T*F{strcat(Tstring,Fstring);
Strcat(Tstring, “*”);}
If strcpy(Tstring,Fstring);}
F:id{Fstring(0)=$}
%%
main()
{
yyparse();
}
yyerror()
{
if(“Error”)
}
Running lex and yacc files
- Lex abc.l
- Yacc_d.abc.y
- cc lex.yy.cc.c. tab.c_l
- la.out
- What is LEX & YACC?
- Write functions of LEX & YACC?
- Explain the steps to execute the LEX program.
- Explain different sections present in the program of LEX.
Comments
Post a Comment