Upload
vishnu0751
View
215
Download
0
Embed Size (px)
Citation preview
7/30/2019 Compiler Pro
1/15
INDEX
1. Develop a lexical analyzer to recognize a few patterns inPASCAL & C.
2. Develop an LL (1) Parser.
3. Develop an operator precedence parser.
4. Develop Recursive Descent parser.
5. Write a Program for Generating for variousintermediate code form:-
A.)Three-address code
B.)Polish notation
7/30/2019 Compiler Pro
2/15
Q. LEXICAL ANALYZER TO RECOGNIZE FEW
PATTERNS IN PASCAL & C
#include#include#include#include#includeint bgn-ptr=O,fwd-ptr=O;char sym_tb[50],str[50],cnst_tb[50],keywords[50];void status(char temp[]){
if(strlen(temp)==0)printf(none found.\n);}void enter(int cnt){int i;J=O,,char tmp[50];for(i=bgn-ptr;i
7/30/2019 Compiler Pro
3/15
strcat(cnst_tbl,tbl);}int lookup(char temp[]){int I;for(i=0;i
7/30/2019 Compiler Pro
4/15
if(str[fwdytr+ 1]==){opr_tb1[opr_cnt++]=str[++fwd-ptr];if(fwd-ptr!=bgn-ptr)enter(fwd-ptr-1);}elseif(fwd-ptr!=bgn-ptr)enter(fed-ptr);
bgn-ptr=fwd+ptr+1;opr_tb1[opr+cnt++]=\n;opr+tb1[opr_cnt]=\0;}else if(str[fwd+ptr]=;)
{if(fwd-ptr!=bgn-ptr)enter(fwd-ptr);
bgn=ptr=fed-ptr+1;}}clrscr();
printf(the statement was:%s\n,str);printf(list of identifiers used is as follows :\n%ssym_tbl);status(sym_tbl);
printf(list of identifiers used is as follows :\n%scnst_tbl);status(cnst_tbl);
printf(list of identifiers used is as follows :\n%sopr_tbl);status(opr_tbl);
printf(list of identifiers used is as follows :\n%s,keywords);status(keywords);getch();}
OUTPUT:
The statement was: a=b+c*10-if;List of identifiers used is as follows:a
bc
7/30/2019 Compiler Pro
5/15
Q. DEVELOP AN LL (1) PARSER
LL(1) Grammar:- A grammar whose parsing table has no multiple
define entries is said to be LL(1). The first L in LL (1) stands for scanningthe input from left to right. The second L for producing a leftmostderivation, and the 1 for using one input symbol of look ahead at each stepto make parsing action decisions.
LL(1) grammar have several distinctive propertiesno ambiguous G is recursive grammar can be LL(1). It can also be shownthat a grammar G is LL(1) if and only if whenever A / are twodistinctive production of the following.
1. For no terminal a do both and drive strings beginning with a.2. At most one of and can drive empty string.3. If * , then does not drive any string beginning with a terminal
in Follow (A).Section of productive parsing table.
Input: Grammar G
OUTPUT:Parsing table M
METHOD:
1. For each production A-> of the grammar, do step 2.
2. For each terminal a in FIRST (), and A-> to M [A].
3. If is in FIRST(),add A-> to M[A,b] for each terminal b inFOLLOW(A), If is in FIRST and $ is in FOLLOW(A),add A-> toM[A, $].
4. Make each undefined entry of M be error.
7/30/2019 Compiler Pro
6/15
Q. Develop an Operator precedence parser.
Ans: In operator precedence parsing we define three disjoint precedencerelation , between certain pair of terminals. These precedence
relations guide the selection of handle and have the following meanings:-
Ab: a takes precedence over b.
ALGORITHM :
INPUT: An input string w and a table of precedence relations.
OUTPUT- If w is well formed , a skeletal parse tree, with aplaceholder non-terminalE labeling all interior nodes, otherwise anerror indication.
METHOD- Initially the stack contains $ & the input buffer, the string w$.To parse, we execute the following program for operators precedence
parsing algorithm.
1. Set input to point to the first symbol of w $.2. Repeat.3. If $ is on top of the stack & input points to $ than4. Return
ElseBegin
5. Let a be the topmost terminal symbol on the stack & let b be thesymbol pointed to by input
6. If a
7/30/2019 Compiler Pro
7/15
12. Until the top stack terminal is related by < to the terminal mostrecent popped.
13. Else error ( )End.
7/30/2019 Compiler Pro
8/15
Q. Develop Recursive Decent Parser
Ans. Recursive Decent Parsing is a top down method of syntaxanalysis in which we execute a set of recursive procedure to
process the input. A procedure is associated with each non terminalof a grammar. We have a special form of recursive descent parsingcalled predictive parsing, in which the look a head symbol unambiguously determines the procedure selected for each nonterminal. The sequence of procedure called in processing the inputimplicitly defines a parse tree for the input. A predictive parser is a
program consisting of a procedure for every non terminal. Eachprocedure does two things:-
1. It decides which production to use by looking at the look ahead symbol.
2. The procedure uses a production by mimicking the right side.
ALGORITHM
INPUT: - A string w & a parsing table M for grammar G.
OUTPUT: - If w is in L (G), a left most derivation of w;otherwise, an error indication.
METHOD: - Initially, the parser is in a configuration in which ithas $ S on the stack with S, the start symbol of G on top & w$ inthe input buffer.
1. Set input to point to the first symbol ofw$;2. Repeat3. Let X be the top stack symbol & a the symbol pointed to by input;4. IfX is a terminal or $ then5. IfX=a then6. Pop X from the stack and advance input7. Else error()
7/30/2019 Compiler Pro
9/15
Else8. IfM [x,a]=XY1,Y2,Yk, then
Begin9. Pop X from the stack;
10. Push Yk, Yk-1,.Y1 onto the stack with Y1 on top;11. Output the production
XY1, Y2,.Yk
End12. Else error ()13. Until X=$.
7/30/2019 Compiler Pro
10/15
Q. Three Code Generation.
#include
#includevoid main( ){int a=1,b=2,c=3,d=4,e=5,f=6,t1,t2,t3,t4,t5;clrscr();
printf(Your Expr is(a
7/30/2019 Compiler Pro
11/15
Q. Postfix Notation
#include#include#include#include#define STACKSIZE 100typedef int item_type;typedef struct s_tag{
int TOP;
item_type element[STACKSIZE];
}stack;void initialize(stack *S);void push(stack *,int);int pop(stack *);void postfix_eval();int m,len=0;int q[STACKSIZE];void main()
{
char *str;printf(This program evaluate the postfix expression using stack!\n);
printf(Please enter expression(each character is separated by spacecharacter);
printf(To quit enter fullstop.)\n);gets(str);while(*str)
{
if (IS_DIG(*STR))&&(!IS_SP(*(++STR)--))){q[m]=atoi(str);str=str+2;m++;
7/30/2019 Compiler Pro
12/15
}else{
if(IS_DIG(*(str)))){
q[m]=atoi(str);str=str+1;m++;
}
}
q[m]=(*str);++str;m++;
if(*str==.){
q[m]=(*str);m++;
break;}
}Len=m;postfix_eval();l}
void initialize(stack*S){
S->TOP=0;}
void push(stack*S,int item)
{
if(S->TOP>=STACKSIZE){
printf(\n Stack is full\n);
7/30/2019 Compiler Pro
13/15
}else
{
S->element[S->TOP]=item;S->element=s->TOP+1;}
}int pop(stack*S){int item;
If(s->TOPTOP>=0){
S->TOP=S->TOP-1;item=S->element[s->TOP];
}}return item;
}void postfix_eval()
{int p1,p2,temp;stack S;
initalise(&S);m=0;
while (m
7/30/2019 Compiler Pro
14/15
break;
case+:p1=pop(&S);p2=pop(&S);temp=p2+p1;
push(&S,temp);m++;
break;case- :
p1=pop(&S);p2=pop(&S);temp=p2-p1;
push (&S,temp);
m++;break;
case*:p1=pop(&S);p2=pop(&S);temp=p2*p1;
push(&S,temp);m++;
break;case/:
p1=pop (&S);
p2=pop (&S);temp=p2/p1;
push(&S,temp);m++;
break;
case %:p1=pop (&S);p2=pop (&S);temp=p2%p1;push (&S,temp);m++;break;
7/30/2019 Compiler Pro
15/15
case ^:
p1=pop(&S);p2=pop(&S);temp=pow(p2,p1);push (&S,temp);m++;break;
case.:
temp=pop(&S);printf(The evaluated result of the postfix expression
is:);printf(%d\n,temp);
exit (0);
default:
temp=q[m];push (&S,temp);++m;break;
}}
}
Output:
This program evaluates the postfix expression using stack!Please enter expression (each character is separated by space character,To quit enter full stop.(a+b)+(c-d)ab+cd-+