Reference no: EM132594722
1. Write a Lex input file that will produce a scanner that capitalizes all comments in a C program.
Moreover after replicating its input program in the terminal (or in a file),
the scanner should append the
total number of comments to its output. Your scanner should be able to accept
optional command line
arguments to indicate the input and output filenames. Please consider using
toupper(char c) from
<ctype.h> to convert to uppercase.
2. Write a rule in Lex to print integer numbers that are multiple of five.
The numbers are allowed to have an optional sign (+/-). You are not neither allowed to convert the matched strings into integers, nor to use the modulus operator (%) in your rule.
3. Give only the rule section of a Lex input file for a scanner that prints only all the letters in its input on screen, and ignores everything else.
4. Answer the following:
a) Write down an unambiguous grammar that generates the set of strings { s; , s;s; , s;s;s; , ... }.
The semicolon should be right associative.
b) Give a leftmost and a rightmost derivation for the string s;s; using your grammar.
5. Given the grammar
exp → exp + term | exp - term | term
term → term * factor | factor
factor → ( exp ) | number
a) Write down a parse tree and abstract syntax tree for the expressions 3+4*5-6.
b) On the parse tree, number the nodes to represent a rightmost derivation.
6. Consider the following grammar representing simplified LISP-like expressions
lexp → atom | list
atom → number | identifier
list → ( seq )
seq → seq lexp | lexp
a) Draw a parse tree for the LISP expression (a 23 (m x y)). Number the nodes in the tree to represent a leftmost derivation. There are one number and 4 identifiers in the given expression besides the parentheses.
b) Translate the given grammar into EBNF.
c) Draw syntax diagrams for the EBNF from part (b).
7. Give a grammar that generates the following set of strings
a) { an bn?¹ | n ≥ 0 }.
b) { an?¹cbn | n ≥ 0 }.
8. Given the following grammar
statement → if-stmt | other | ε if-stmt → if ( exp ) statement else-part else-part → else statement | ε exp → 0 | 1
a) Is the grammar ambiguous? Justify your answer (with examples if applicable)
b) Draw a parse tree for the string if(0) if(1) other else else other
c) What is the purpose of the two else's in the given string?