prg 3&4


Aim:To write a program to implement Ambiguous grammar using YACC and LEX.
Grammar: E->E+E | E-E |E*E | E/E|(E)|id

Step 1: Start the program.
Step 2: Declare yyerror, yylval.
Step 3: Include the required header files.                                                                                                                                                
Step 4: In the pattern partition, give rules for constant and operators.
Step 5: In the yacc file, include stdio library and yyerror
               Function
Step 6: In pattern section, give the rule for the following grammar.
              E->E+E | E-E |E*E | E/E|(E)|id
Step 7: Compute the expression values by using ‘$’ symbol
Step 8: In the definition, invoke the parser.
Step 9: Define yyerror function.


CODE:
%{
 #include<stdio.h>
 #include"y.tab.h"
void yyerror(char *);
extern int yylval;
%}
%%
[0-9]+           {yylval=atoi(yytext); return INT;}
[-*+/\n]       {return *yytext;}
[/)/(] {return *yytext;}
.           {yyerror("syntax error");}
%%
int yywrap()
{
return 1;
}
                    


Ambiguous.yacc

%{
 #include<stdio.h>
extern int yylex(void);
void yyerror(char *);
%}
%token INT
%%
program:
program expr '\n'                        {printf("%d\n",$2);}
|
;
expr:
INT                 {$$=$1;}
|expr '+' expr         {$$=$1+$3;}
|expr '-' expr          {$$=$1-$3;}
|expr '*' expr          {$$=$1*$3;}
|expr '/' expr  {$$=$1/$3;}
|'(' expr ')'   {$$=$2;}
%%
void yyerror(cha*s)
{
printf("%s",s);
}
int main()
{
yyparse();
return 0;
}


Result : The program of implementation of Ambiguous using YACC and LEX hasbeen executed successfully.

Output :

2+3
5

Ex.No:4  Unambiguous grammar
Aim:To write a program to implement Unambiguous grammar using YACC and LEX.
Grammar: E->E+T | E-T | T
                     T->T*F |T/F | F
                     F-> (E) | id

Algorithm:
                        Step 1: Start the program.
Step 2: Declare yyerror, yylval.
Step 3: Include the required header files.                                                                                                                                                
Step 4: In the pattern partition, give rules for constant and operators.
Step 5: In the yacc file, include stdio library and yyerror
               Function
Step 6: In pattern section, give the rule for the following grammar.
              E->E+T | E-T | T
              T->T*F | T/F | F
               F-> (E) | id
Step 7: Compute the expression values by using ‘$’ symbol
Step 8: In the definition, invoke the parser.
Step 9: Define yyerror function.

Program:  unamb.l
%{
 #include<stdio.h>
 #include"y.tab.h"
void yyerror(char *);
extern int yylval;
%}
%%
[0-9]+           {yylval=atoi(yytext); return id;}
[-*+/\n\(\)]           {return *yytext;}
.                       {yyerror("syntax error");}
%%
int yywrap()
{
return 1;
}

unamb.y
%{
 #include<stdio.h>
extern int yylex(void);
void yyerror(char *);
%}
%token id
%%
p:
p  E  '\n'                   {printf("%d\n",$2);}
|
;
E:
T                     {$$=$1;}
|E  '+'  T        {$$=$1+$3;}
|E  '-'  T         {$$=$1-$3;}
;
T:
F                      {$$=$1;}
|T '*' F           {$$=$1*$3;}
|T '/' F           {$$=$1/$3;}
;
F:
id                    {$$=$1;}
|'(' E ')'         {$$=$2;}
%%
void yyerror(char *s)
{
printf("%s",s);
}
int main()
{
yyparse();
return 0;
}
Result : The program of implementation of Unambiguous  using YACC and LEX has been executed successfully.
Output :
2+5-1

Comments

Popular posts from this blog

operator precendence parsing