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

18 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
  6. Hello! Thanks for sharing knowledgeable post, I have read it very carefully. I just found this post very effective to make the concept strengthen.
    I wanted to request you please visit my website, I have also post the knowledgeable information. If you need help with the writing essays are also offering writing services. You can contact us: info@gradesmaster.com Here is the Url of my website: https://gradesmaster.com/

    ReplyDelete
  7. This comment has been removed by the author.

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

    ReplyDelete
  9. Tula's International School is the best Dehradun boarding schools for girls & boys. It is one of the top schools in Dehradun.The school is affiliated to CBSE which offers holistic education to students.

    Tula's International School Best Boarding School in Dehradun

    Tula's International School Best CBSE Boarding School in Dehradun

    Tula's International School Best Boarding School in Dehradun

    Tula's International School Co-ed Boarding School in Dehradun

    Tula's International School Best Residential School in Dehradun

    Tula's International School Dehradun Boarding School Fee structure

    Tula's International School Top Girls Boarding School India

    Tula's International School Best CBSE Schools in Uttarakhand

    Tula's International School Top Boarding Schools in India

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

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...