N.K. Srinathsrinath_nkyahoo.com 1 RVCE - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

N.K. Srinathsrinath_nkyahoo.com 1 RVCE

Description:

fprintf(stderr, 'could not open %sn', argv[1]); exit(1); yyin = file; yylex ... file = fopen(argv[1], 'r'); if (!file) printf('error opeing a file n'); exit(1) ... – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 41
Provided by: Srin2
Category:

less

Transcript and Presenter's Notes

Title: N.K. Srinathsrinath_nkyahoo.com 1 RVCE


1
Lex programs
  • Number of words, characters, blanks and lines.
  • Number of comments lines in C.
  • Number of brackets in C
  • Replace scanf with READ and printf with WRITE
  • Expression is valid

N.K. Srinath srinath_nk_at_yahoo.com 1
RVCE
2
  • YACC programs
  • To recognize an valid variable
  • LEX YACC
  • Recognize an valid variable
  • Recognize the grammar anb for n gt 0.

N.K. Srinath srinath_nk_at_yahoo.com 2
RVCE
3

N.K. Srinath srinath_nk_at_yahoo.com 3
RVCE
4

1. Write a Lex program to count the number of
words, characters, blanks and lines in a given
text.
int charcount0 int wordcount0
int linecount0 int blankcount
0 Word \t\n eol \n
The variables are initialized in the definition
section.
Reads the next token when it comes across a
character and considers it as a word when it
comes across a blank, tab and newline.
When newline is found identify it as a eol.
N.K. Srinath srinath_nk_at_yahoo.com 4
RVCE
5

blankcount word wordcount
charcountyyleng eol charcount
linecount . ECHO charcount main(argc
, argv) int argc char argv if(argc gt
1) FILE file file fopen(argv1,"r")
if(!file)
When blank is found increment blankcount, and
when word is found increment wordcount and
charcount is incremented by taking the length of
the word using yyleng function.
When eol is encountered incremtn charcount and
linecount.
For all the other character echo on the screen
and increment charcount.
N.K. Srinath srinath_nk_at_yahoo.com 5
RVCE
6

fprintf(stderr, "could not open
s\n", argv1) exit(1)
yyin file yylex()
printf("\nThe number of characters u\n",
charcount) printf("The number of
wordcount u\n", wordcount)
printf("The number of linecount u\n",
linecount) printf("The number of
blankcount u\n", blankcount)
return(0) else printf(" Enter the
file name along with the program \n")
N.K. Srinath srinath_nk_at_yahoo.com 6
RVCE
7

2. Write a lex program to find the number of
comments lines in a C program.
The solution is to first know the process of
identifying the comment lines in C-language. A
line that with optional text between the / and
/ is a comment line. The rule for the above is
\t/./ \t\n
N.K. Srinath srinath_nk_at_yahoo.com 7
RVCE
8

The comments can begin as follows Int counter /
this is a comment statement / This requires a
different type of rule ././.\n / this
is a comment statement / Int counter
././.\n Beginning of a comment is
recognized \t/
N.K. Srinath srinath_nk_at_yahoo.com 8
RVCE
9

While in the COMMENT state to look for
newlines ltCOMMENTgt\n
When we come to a line which ends with a
comment ltCOMMENTgt / Hence the solution will be
as follows int comments
N.K. Srinath srinath_nk_at_yahoo.com 9
RVCE
10

x COMMENT \t/ BEGIN COMMENT
\t/./ \t\n comments ltCOMMENTgt/
\t\n ltCOMMENTgt/ ltCOMMENTgt\n
comments ltCOMMENTgt.\n comments
N.K. Srinath srinath_nk_at_yahoo.com 10
RVCE
11

././.\n BEGIN COMMENT .
/ ignore everything else/ Main()
yylex() printf(The number of comment lines
d, comments)
N.K. Srinath srinath_nk_at_yahoo.com 11
RVCE
12

3. Write a lex program to find
the given c program has right
number of
brackets. Count the
number of comments. Check for while loop.
/ find main, comments, , (, ), / int
comments0 int opbr0 int clbr0 int
opfl0 int clfl0 int j0 int k0
Opbr open brackets clbr
closed brackets opfl open flower
brackets clfl closed flower brackets
N.K. Srinath srinath_nk_at_yahoo.com 12
RVCE
13

"main()" j1 "/" \t. \t"/"
comments "while("0-9a-zA-Z")" \t\n""
\t."" k1 \t"" \t\n \t""
k1 "("
opbr ")" clbr "" opfl "" clfl
\t\n . ECHO
N.K. Srinath srinath_nk_at_yahoo.com 13
RVCE
14

main(argc, argv) int argc char argv
if (argc gt 1) FILE file file
fopen(argv1, "r") if (!file)
printf("error opeing a file \n")
exit(1)
N.K. Srinath srinath_nk_at_yahoo.com 14
RVCE
15

yyin file yylex() if(opbr !
clbr) printf("open brackets is not equal
to close brackets\n") if(opfl !
clfl) printf("open flower brackets is not equal
to close flower brackets\n") printf(" the
number of comments d\n",comments) if
(!j) printf("there is no main function \n")
N.K. Srinath srinath_nk_at_yahoo.com 15
RVCE
16

if (k) printf("there is loop\n")
else printf("there is no valid for loop\n")
return(0) 4. Write a lex program to replace
scanf with READ and printf with WRITE statement
also find the number of scanf and printf. int
pc0,sc0
pcprintf count scscanfcout
N.K. Srinath srinath_nk_at_yahoo.com 16
RVCE
17

printf fprintf(yyout,"WRITE")pc scanf
fprintf(yyout,"READ")sc . ECHO main(int
argc, char argv ) if(argc!3)
printf("\nUsage s ltsrcgt ltdestgt\n",argv0)
return
N.K. Srinath srinath_nk_at_yahoo.com 17
RVCE
18

yyinfopen(argv1,"r") yyoutfopen(argv2,"w"
) yylex() printf("\nno. of printfs d\ n
no. of scanfs d \n", pc,sc) 5. Write a lex
program to find whether the given expression is
valid. include ltstdio.hgt int
valid0,ctr0,oc 0
N.K. Srinath srinath_nk_at_yahoo.com 18
RVCE
19

NUM 0-9 OP /- NUM(OPNUM)
valid 1
for(ctr 0yytextctrctr)
switch(yytextctr)
case '' case
'-' case ''
case '/' oc

N.K. Srinath srinath_nk_at_yahoo.com 19
RVCE
20

NUM\n
printf("\nOnly a number.") \n if(valid)
printf("valid \n operatorcount d",oc)
else printf("Invalid") valid oc
0ctr0 main() yylex()
N.K. Srinath srinath_nk_at_yahoo.com 20
RVCE
21

6. Write a yacc program to
recognize an valid variable
which starts with letter followed by a digit.
The letter should be in lowercase only.
include ltstdio.hgt include ltctype.hgt
N.K. Srinath srinath_nk_at_yahoo.com 21
RVCE
22

token LETTER DIGIT st st LETTER DIGIT '\n'
printf("\nVALID") st '\n' error
'\n' yyerror("\nINVALID")yyerrok
main() yyparse()
N.K. Srinath srinath_nk_at_yahoo.com 22
RVCE
23

yylex() char c while((cgetchar())'
') if(islower(c)) return LETTER if(isdigit(c))
return DIGIT return c yyerror(char s)
printf("s",s)
N.K. Srinath srinath_nk_at_yahoo.com 23
RVCE
24
  • LEX produces a function called yylex()
  • YACC produces a function called yyparse()
  • yyparse() expects to be able to call yylex()
  • How to get yylex()?
  • Write your own!
  • If you don't want to write your own Use LEX!!!

N.K. Srinath srinath_nk_at_yahoo.com 24
RVCE
25

int yylex() if(it's a num) return NUM else
if(it's an id) return ID else if(parsing is
done) return 0 else if(it's an
error) return -1
N.K. Srinath srinath_nk_at_yahoo.com 25
RVCE
26

LEX YACC
N.K. Srinath srinath_nk_at_yahoo.com 26
RVCE
27

LEX and YACC a team
call yylex()
0-9
next token is NUM
NUM NUM
N.K. Srinath srinath_nk_at_yahoo.com 27
RVCE
28

Building Example
Suppose you have a lex file called scanner.l and
a yacc file called decl.y and want parser Steps
to build... lex scanner.l yacc -d
decl.y gcc -c lex.yy.c y.tab.c gcc -o parser
lex.yy.o y.tab.o -ll Note scanner should
include in the definitions section include
"y.tab.h"
N.K. Srinath srinath_nk_at_yahoo.com 28
RVCE
29

Symbol attributes
  • Every symbol can have a value
  • Might be a numeric quantity in case of a
    number (42)
  • Might be a pointer to a string ("Hello,
    World!")
  • Might be a pointer to a symbol table entry in
    case of a variable
  • When using LEX we put the value into yylval
  • In complex situations yylval is a union
  • Typical LEX code
  • 0-9 yylval atoi(yytext) return NUM

N.K. Srinath srinath_nk_at_yahoo.com 29
RVCE
30

YACC allows symbols to have
multiple types of value
symbols union double dval int
vblno char strval
N.K. Srinath srinath_nk_at_yahoo.com 30
RVCE
31

Symbol attributes
union double dval int
vblno char strval
yacc -d
y.tab.h extern YYSTYPE yylval
0-9 yylval.vblno atoi(yytext)
return NUM A-z yylval.strval
strdup(yytext) return STRING
LEX file include y.tab.h
N.K. Srinath srinath_nk_at_yahoo.com 31
RVCE
32

7. Write a yacc program to recognize
an valid variable which starts with
letter followed by a
digit. The letter should be in lowercase only. /
Lex program to send tokens to the yacc program
/ include "y.tab.h" 0-9 return
digit a-z return letter \n return
yytext0
N.K. Srinath srinath_nk_at_yahoo.com 32
RVCE
33

. return 0 / Yacc program to validate the
given variable / includelttype.hgt
token digit letter
N.K. Srinath srinath_nk_at_yahoo.com 33
RVCE
34

ident expn '\n' printf ("valid\n")
exit (0) expn letter expn
letter expn digit error yyerror
("invalid \n") exit (0) main() yypar
se()
N.K. Srinath srinath_nk_at_yahoo.com 34
RVCE
35

yyerror (char s) printf("s", s)
N.K. Srinath srinath_nk_at_yahoo.com 35
RVCE
36

8. Write a yacc program to recognize
the grammar anb for n gt
0. / Lex program to pass tokens to yacc program
/ include "y.tab.h" a return a
printf("returning A to yacc \n") b return
b \n return yytex0 . return error
N.K. Srinath srinath_nk_at_yahoo.com 36
RVCE
37

/Yacc program to check the given
expression / includeltstd
io.hgt token a b error input line
error line expn '\n' printf(" valid new
line char \n")
N.K. Srinath srinath_nk_at_yahoo.com 37
RVCE
38

expn aa expn bb a aa aa a a bb
b error error yyerror ( " " )
N.K. Srinath srinath_nk_at_yahoo.com 38
RVCE
39

main() yyparse() yyerror (char
s) printf("s", s)
N.K. Srinath srinath_nk_at_yahoo.com 39
RVCE
40

Thank You
N.K. Srinath srinath_nk_at_yahoo.com 40
RVCE
Write a Comment
User Comments (0)
About PowerShow.com