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 ~ $
// 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 ~ $
conflicts: 25 shift/reduce, 4 reduce/reduce
ReplyDeleteI'm also getting this error:
ReplyDeletefor.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)
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.
Deleteim a potato
ReplyDeleteim a tomato
Deletei'm a zomato
Deletei am ropato
Deletei am potato
Deletei am pomato
Deleteim a kali linux
Deletegot compiler design exam tomorrow, im so cooked dawg
Deleteerrorrrrrrrrrrrrrrrr
ReplyDeletewhy the error
ReplyDeleteAlthough 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.
ReplyDeleteError
ReplyDeleteyes good work
ReplyDeleteERROR
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis code doesnt run the for loop, it just checks the grammar. nothing else.
ReplyDeleteyes i need to execute the for loop
DeleteVery nice article. Also check out Tech no Kami !
ReplyDeleteThis 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.
ReplyDeletewebsite development company in Surat Gujarat
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.
ReplyDeletetransfer large file
Respect and that i have a dandy present: kitchen refurbishment
ReplyDeleteGood. It is working properly. Ty.
ReplyDelete