Lex ( lexprg.zip - 5 KB)

1) Count vowels and consonants in a string.
2) Count chars,words,spaces and lines in a given input file.
3) Count : a) +ve and -ve ints
________b) +ve and -ve fractions
________ Combined Program (expected program)
4) Count comment lines in a C Program. Eliminate them and copy rest of the code to another file.
5) Count scanf and printf in a C program. Replace them with readf and writef.
6) Recognize a valid arithmetic expression and identify the identifiers and operators present. Print them seperately.
7) Recognize whether a given sentance is simple or compound.
8) Count number of identifiers in a given input file.

Yacc (yaccprg.zip - 5 KB)

1) Test validity of simple expression involving + - * /
2) Recognize nested IF statements and display the levels of nesting.
3) Recognize a valid arithmetic expression that uses operators + - * /
4) Recognize a valid variable, which starts with a letter, followed by any number of letters or digits.
5) Evaluate an arithmetic expression involving operators + - * /
6) Recognize strings using the grammar (a^n b^n , n>=0)
7) Recognize the grammar (a^n b, n>=10).

 

Notes and Tips :

The default yywrap() function is defined in all lex programs above as :

yywrap() { return 1; }

This is not actually necessary when gcc is used with the -ll option, but if the (f)lex library is not available for use with your compiler, (or if you are running these scripts on Windows), then you must include the definition of this function.

===

Visit :
http://www.monmouth.com/~wstreett/lex-yacc/lex-yacc.html
to get Flex and Bison for Windows. You can use them with Borland TurboC 3.0 or GCC for Windows or any other standard compiler.
Note : In DOS, CTRL+D doesn't work. So to terminate input while running your executable, use the CTRL+Z.

===

Use this in the yacc program if u have only one type of token wich is not a string :
#define YYSTYPE double
This might be used in the expression evaluation programs.

===

Instead of using Lex to scan, use your own handwritten lexer (in the .y file) like :

yylex()
{ char c;
while((c=getchar())==' '); //inhibit spaces

// to return numbers
if(isdigit(c))
{ ungetc(c,stdin);
scanf("%lf",&yylval);
return NUMBER;
}

//return all the remaining chars too.
return c;
}

Hosted by www.Geocities.ws

1