Parser for FOR Loop Statements - YACC Program - Compiler Design

Program:

// Lex file: for.l

alpha [A-Za-z]
digit [0-9]

%%

[\t \n]
for             return FOR;
{digit}+    return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="         return LE;
">="         return GE;
"=="         return EQ;
"!="          return NE;
"||"            return OR;
"&&"         return AND;
.                return yytext[0];

%%


// Yacc file: for.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM FOR LE GE EQ NE OR AND
%right "="
%left OR AND
%left '>' '<' LE GE EQ NE
%left '+' '-'
%left '*' '/'
%right UMINUS
%left '!'

%%
   
S         : ST {printf("Input accepted\n"); exit(0);}
ST       : FOR '(' E ';' E2 ';' E ')' DEF
           ;
DEF    : '{' BODY '}'
           | E';'
           | ST
           |
           ;
BODY  : BODY BODY
           | E ';'       
           | ST
           |            
           ;
       
E        : ID '=' E
          | E '+' E
          | E '-' E
          | E '*' E
          | E '/' E
          | E '<' E
          | E '>' E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | E '+' '+'
          | E '-' '-'
          | ID 
          | NUM
          ;

   
E2     : E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         ;   
%%

#include "lex.yy.c"
main() {
    printf("Enter the expression:\n");
    yyparse();
}     
     


Output:
nn@linuxmint ~ $ lex for.l
nn@linuxmint ~ $ yacc for.y
conflicts: 25 shift/reduce, 4 reduce/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression:
for(i=0;i<n;i++)
i=i+1;
Input accepted
nn@linuxmint ~ $

10 comments:

  1. conflicts: 25 shift/reduce, 4 reduce/reduce

    ReplyDelete
  2. I'm also getting this error:

    for.l: In function ‘yylex’:
    for.l:7:8: error: ‘FOR’ undeclared (first use in this function)
    for.l:7:8: note: each undeclared identifier is reported only once for each function it appears in
    for.l:8:8: error: ‘NUM’ undeclared (first use in this function)
    for.l:9:8: error: ‘ID’ undeclared (first use in this function)
    for.l:10:8: error: ‘LE’ undeclared (first use in this function)
    for.l:11:8: error: ‘GE’ undeclared (first use in this function)
    for.l:12:8: error: ‘EQ’ undeclared (first use in this function)
    for.l:13:8: error: ‘NE’ undeclared (first use in this function)
    for.l:14:8: error: ‘OR’ undeclared (first use in this function)
    for.l:15:8: error: ‘AND’ undeclared (first use in this function)

    ReplyDelete
  3. I really like your good infomation,Thanks and please keep sharing it with us.
    business consulting services oman

    ReplyDelete
  4. Although I landed on this page accidentally while I was looking for a professional research writer to Redo my MBA Thesis Proposal, I have learned a lot about looping and I will bookmark this site so that I can visit it during my free time and practically practice to write the programs.

    ReplyDelete
  5. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in dot net programming your post help me a lot.Thanks for your informative article.Dot Net training in chennai | dot net training and placement | Dot Net training in velachery

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...