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 ~ $

22 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
    Replies
    1. This happened to me when I gave #include "lex.yy.c" in first declaration. But when I moved it near to main() then those errors got vanished.

      Delete
  3. 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
  4. This comment has been removed by the author.

    ReplyDelete
  5. This code doesnt run the for loop, it just checks the grammar. nothing else.

    ReplyDelete
  6. Very nice article. Also check out Tech no Kami !

    ReplyDelete
  7. This article is a great article that I have seen in my blog career so far, it helps me a lot in my loop programming career, and will continue to do so in the future.

    website development company in Surat Gujarat

    ReplyDelete
  8. If you want to transfer files for free over internet of cost . You can check Sendbig.com for sharing a photos, video and other files over internet free of cost. You can send 10gb file free in video, email and other files on internet.
    transfer large file

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...