Lexyaccexamples

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

BITS PILANI – DUBAI CAMPUS II Semester 2004 – 2005 III Yr. C.S.

Course: CSUC362 Programming Languages and Compiler Construction


Individual Assignment: Total Marks: 20 Weightage: 10 %
Date for Demonstration and Record Submission: On or Before 21/03/05 Monday
Record should contain LEX & YACC Source Code along with OUTPUT.
Note: Delayed Subission will result in reduction in marks.
Copying not permissible. Issue Date: 27/02/05

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.

Valid Inputs are shown here:


for(i = 1; i <= 20; i+=1) sum = sum + i;

for(i = 1; i <= 20;) sum += sum + i;

for(i = 1; ; i += 1) sum = sum - i;

for(i = 1; ;) sum = sum + i;

for( ; i >= 4; i -= 2) sum -= i;

for( ; i <= a; ) sum *= i;

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:

“YOUR STATEMENT is SYNTACTICALLY CORRECT”.

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:

“YOUR STATEMENT SYNTAX IS WRONG”.


BITS PILANI – DUBAI CAMPUS II Semester 2006 – 2007 III Yr. C.S.
Course: CSUC362 Programming Languages and Compiler Construction
Individual Assignment: Total Marks: 10 marks
Evaluation will be based on Individual effort, Timeliness, Demonstration and Viva.
Date for Demonstration and Record Submission: On or Before 10/04/2007(Tue.)
Record should contain LEX & YACC Source Code along with OUTPUT & INPUT.
Note: Delayed Submission will result in reduction in marks.
Copying not permissible and will not be considered. Issue Date: 20/03/2007.
The students are required to work in their individual linux accounts in the linux server.
Use input redirection ( < read data from a file )
and output redirection ( > write results to a file ).
3. Using LEX and YACC, Check whether a given “SQL” Query statement (read
from input text file) entered by the user , is syntactically correct according to
SQL Syntax. You can make the following assumptions:

a)The operators considered are: = < <= > >=


b) A constant can be integer or real or string (enclosed in double quotes).
c) The query can be according to any one of the following syntax:

1) select * from relname

2) select * from relname where relname.fieldname operator constant

3) select * from relname where relname.field1name operator constant


and relname.field2name.operator constant
Note: relname, fieldname, field1name, field2name denote identifiers. * , . and
terminals/keywords are in bold.
Valid Test Inputs are shown here:(you must create separate input file for each test case )
a) select * from studmaster
b) select * from studmaster where studmaster.idno=10002
c) select * from studmaster where studmaster.cgpa >= 6.0
d) select * from studmaster where studmaster.cgpa <= 6.0
e) select * from studmaster where studmaster.idno > 1000 and studmaster.cgpa <=
4.5
f) select * from studmaster where studmaster.name = “shivajiganesan”
For each of the above inputs, you can echo the statement and display
the following message on screen:
“YOUR STATEMENT is SYNTACTICALLY CORRECT”.

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:

“YOUR STATEMENT IS WRONG SYNTACTICALLY; TRY WITH CORRECT SYNTAX”.


CSUC362 PLCC Lab. Staff I/C: B.V. Kumar
Practice on 4 function CALCULATOR involving whole numbers
You can use VI editor to create LEX/YACC Source files
-------------------------------------------------------------
Contents of file: calcu.l (This is the lex source file)
%{
#include "y.tab.h"
#include <ctype.h>
extern int yylval;
%}
%%
[0-9]+ {
yylval = atoi(yytext);
return(NUMBER);
}
"+"|"-"|"*"|"/"|"("|")"|"\n" { return(yytext[0]); }
%%
-------------------------------------------------------------
Contents of the file calcu.y (This is the yacc source file)
%{
#include <stdio.h>
%}
%token NUMBER,")","("
%left "+", "-"
%left "*", "/"
%%
list: /* Nothing */
| list expr "\n" { printf("Result: %d\n", $2); }
| list "\n"
;
expr: NUMBER { $$ = $1; }
| expr "+" expr { $$ = $1 + $3; }
| expr "-" expr { $$ = $1 - $3; }
| expr "*" expr { $$ = $1 * $3; }
| expr "/" expr { $$ = $1 / $3; }
| "(" expr ")" { $$ = $2; }
;
%%
main()
{
yyparse();
}
yyerror()
{
printf("ERROR: Aborting Execution\n");
exit(1);
}
-----------------------------------------------------
Compilation, Linking and Execution Steps:
1. lex calcu.l
2. yacc -d calcu.y
3. cc -c lex.yy.c
4. cc -c y.tab.c
5. cc lex.yy.o y.tab.o -ll -o calcu
6. calcu
Now you can give test data and get results in next line
Test Data: 4+5 (4+5) 4-5
(1+2+3)*(4+5+6) 21/3 (5*3)/3
You can press Ctrl d or Ctrl c to exit the program

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);
}

You can use input redirection to read input from a file:


Ex: myprg < infile

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;
}

%token <number> INTG


%token <string> FOR
%token <name> ID
%token <operator> OP
%token <lgt> LG
%token <equalto> EQ
%token <bracopen> BO
%token <bracclose> BC
%token <semicolon> SC

%%
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:

Example : forloop < in1 forloop < in7

for ( i = 1 ; i <= 20; i += 1) sum = sum + i;


for ( i = 1 ; i <= 20; ) sum += sum + i;
for ( i = 1; ; i += 1 ) sum = sum - i;
for ( i = 1; ;) sum = sum + i;
for ( ; i >= 4; i -= 2) sum -= i;
for ( ; i <= a; ) sum += i;
for ( ; ; i += 1 ) sum /= i;
for ( ; ; ) sum += i;

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);
}

Test data : store each query in a separate file.


select * from gaurav
select * from gaurav where gaurav.idno=10002
select * from gaurav where gaurav.cgpa >= 6.0
select * from gaurav where gaurav.cgpa <= 6.0
select * from gaurav where gaurav.idno > 1000 and gaurav.cgpa <= 4.5
select * from gaurav where gaurav.name = "gauravu"
select where student

You might also like