Compiler Desing-Final ppt2
Compiler Desing-Final ppt2
declarations
%%
Translation rules
%%
Auxiliary functions (optional)
• The declaration section includes declarations
of
– variables, manifest constants ( identifiers declared
to stand for a constant), and
– regular definitions.
%%
“HI” printf(“Hello World”);
. ; //Empty C statement.
Does nothing for any other character
encountered in the input
%%
Executing the Lex File
• lex example1.l
(Processes the lex file to generate a scanner which
gets saved as lex.yy.c)
• cc lex.yy.c –ll
compile the scanner and grab main() from the lex
library(-ll)
• ./a.out
Run the scanner taking input from standard input
Fig: Lex program for the relational operators as
tokens
• For a trivial example, consider a program to delete
from the input all blanks or tabs at the ends of
lines.
%%
[ \t]+$ ;
is all that is required.
"\[]^-?.*+|()$/{}%<>
and if they are to be used as text characters, an
escape should be used.
• The quotation mark operator (") indicates that
whatever is contained between a pair of quotes is to
be taken as text characters. Thus
xyz"++"
matches the string xyz++ when it appears.
– Note that a part of a string may be quoted. It is harmless
but unnecessary to quote an ordinary text character; the
expression
"xyz++"
is the same as the one above.
• An operator character may also be turned into
a text character by preceding it with \ as in
xyz\+\+
which is another, less readable, equivalent of
the given expressions.
• In character classes, the ^ operator must appear as
the first character after the left bracket; it indicates
that the resulting string is to be complemented with
respect to the computer character set. Thus
[^abc]
matches all characters except a, b, or c, including all
special or control characters; or
[^a-zA-Z]
is any character which is not a letter.
• Lex Actions:
Derivations
• The construction of a parse tree can be made
precise by taking a derivational view, in which
productions are treated as rewriting rules.
• At each step, we choose a non-terminal to
replace. Different choices can lead to different
derivations.
Abstract Syntax
Continuous
Stream of Parser Tree, Syntax
Tree,
Tokens
intermediate
code, etc.
Syntax + Translation
Rules
– Syntax-Directed Definitions
– Translation Schemes
• Syntax-Directed Definitions:
• Synthesized attributes
• Inherited attributes
Eg. a= (22/7)*d
2. Constant propagation
Before Optimization
6. Strength Reduction
• It is the replacement of expressions that
are expensive with cheaper and simple ones.
Example:
B=A*2; B=A+A;
Before After
Optimization Optimization
Basic Blocks and Flow Graphs
• Helps in the identification of loops in the code
for code optimization process