Professional Documents
Culture Documents
Lexyaccexamples
Lexyaccexamples
Lexyaccexamples
1. Using LEX and YACC, Check whether a given “for” statement (read from
standard input / text file) entered by the user , is syntactically correct according to
C Language Syntax. You can make the following assumptions:
i) The assignment operators to be considered are: = += -= *= /=
ii) Arithmetic operators to be considered are: + - * /
iii) An expression can have ( ) identifiers numbers(only positive integers).
iv) The relational operators considered are: < <= > >=
v) Assume that there can be only one loop variable.
vi) Assume that there are no compound statements. Only assignment expression /
statement is considered for simplicity.
for( ; ; i += 1) sum /= i;
for( ; ; ) sum += i;
For each of the above inputs, you can echo the statement and display
the following message on screen:
Also, try giving your input with some mistakes [like type some junk,
missing:- bracket, variable, operator etc]. In this case, you can echo
the statement and display the following message on screen:
Also, try giving your input with some mistakes [like type some junk,
missing:- keyword, operator, identifier etc]. In this case also, you
can echo the statement and display the following message on screen:
This lex program counts words, lines and characters from a input file
=======================================================================
%{
unsigned cc=0, wc=0, lc=0;
%}
word [^ \t\n]+
eol \n
%%
{word} { wc++; cc+= yyleng; }
{eol} { cc++; lc++ ; }
. cc++ ;
%%
main()
{ yylex();
printf("%d %d %d", lc, wc, cc);
}
This is previous years assignment for for loop syntax checking. LEX
and YACC sources are shown below:
%{
#include<stdio.h>
#include "y.tab.h"
#include<ctype.h>
#include<string.h>
%}
%%
for {
yylval.string = strdup(yytext);
return FOR;
}
[0-9]+ {
yylval.number = atoi(yytext);
return(INTG);
}
[a-zA-Z][a-zA-Z0-9]* {
yylval.name = strdup(yytext);
return(ID);
}
"/" |
"-" |
"+" |
"*" {
yylval.operator = strdup(yytext);
return(OP);
}
"=" {
yylval.equalto = strdup(yytext);
return(EQ);
}
"(" {
yylval.bracopen = strdup(yytext);
return(BO);
}
")" {
yylval.bracclose = strdup(yytext);
return(BC);
}
"<" |
">" {
yylval.lgt = strdup(yytext);
return(LG);
}
";" {
yylval.semicolon = strdup(yytext);
return(SC);
}
[ \n\t] {;
}
%%
------------------------------------------------------
%{
#include<stdio.h>
#include<string.h>
#include<ctype.h>
%}
%union {
int number;
char *string;
char *name;
char *operator;
char *lgt;
char *equalto;
char *bracopen;
char *bracclose;
char *semicolon;
}
%%
list :
| FOR BO SC SC BC ID OP EQ ID SC
{ printf("\nStatement Correct!"); }
| FOR BO SC SC ID OP EQ INTG BC ID OP EQ ID SC
{ printf("\nStatement Correct!"); }
| FOR BO SC ID LG EQ ID SC BC ID OP EQ ID SC
{ printf("\nStatement Correct!"); }
| FOR BO SC ID LG EQ INTG SC ID OP EQ INTG BC ID OP EQ ID SC
{ printf("\nCorrect"); }
| FOR BO ID EQ INTG SC SC BC ID EQ ID OP ID SC
{ printf("\nCorrect"); }
| FOR BO ID EQ INTG SC SC ID OP EQ INTG BC ID EQ ID OP ID SC
{ printf("\nCorrect"); }
| FOR BO ID EQ INTG SC ID LG EQ INTG SC BC ID OP EQ ID OP ID SC
{ printf("\nCorrect"); }
| FOR BO ID EQ INTG SC ID LG EQ INTG SC ID OP EQ INTG BC ID EQ ID
OP ID SC
{ printf("\nCorrect"); }
;
%%
main()
{
yyparse();
}
yyerror()
{
printf("\nWrong!");
exit(1);
}
These are the test data each stored in a separate file and tested one
at a time using input redirection:
This is also previous year assignment to check SQL query syntax. The
lex, yacc source and input data [store each query in a separate file
and test each input separately using input redirection:
Example : sqlsyntaxchecker < q1 sqlsyntaxchecker < q7
%{
#include<stdio.h>
#include "y.tab.h"
#include<string.h>
%}
%%
select {
yylval.string = strdup(yytext);
return SELECT;
}
"*" {
yylval.star = strdup(yytext);
return(ST);
}
from {
yylval.string = strdup(yytext);
return FROM;
}
where {
yylval.string = strdup(yytext);
return WHERE;
}
and {
yylval.string = strdup(yytext);
return AND;
}
[a-zA-Z0-9".]* {
yylval.name = strdup(yytext);
return(NAME);
}
"=" {
yylval.equalto = strdup(yytext);
return(EQ);
}
"<" |
">" |
"<=" |
">=" {
yylval.compare = strdup(yytext);
return(COMPARE);
}
[\n\t] {;
}
%%
----------------------------------------------------------
%{
#include<stdio.h>
#include<string.h>
#include<ctype.h>
%}
%union {
char *string;
char *star;
char *name;
char *equalto;
char *compare;
}
%token <string> SELECT
%token <star> ST
%token <string> FROM
%token <name> NAME
%token <string> WHERE
%token <equalto> EQ
%token <compare> COMPARE
%token <string> AND
%%
list :
| SELECT ST FROM NAME
{
printf("%s %s %s %s",$1,$2,$3,$4);
printf("\nSYNTAX CORRECT!");}
| SELECT ST FROM NAME WHERE NAME EQ NAME
{
printf("%s %s %s %s %s %s %s %s",$1,$2,$3,$4,$5,$6,$7,$8);
printf("\nSYNTAX CORRECT!");}
| SELECT ST FROM NAME WHERE NAME COMPARE NAME
{
printf("%s %s %s %s %s %s %s %s",$1,$2,$3,$4,$5,$6,$7,$8);
printf("\nSYNTAX CORRECT!");}
| SELECT ST FROM NAME WHERE NAME COMPARE NAME AND NAME COMPARE NAME
{
printf("%s %s %s %s %s %s %s %s %s %s %s %s",
$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12);
printf("\nSYNTAX CORRECT!");}
%%
main()
{
yyparse();
}
yyerror()
{
printf("\nSyntax is Wrong!");
exit(1);
}