
1
cs480(Prasad) L2Syntax 1
Computer Language Engineering
Computer Language Engineering
โข How to give instructions to a computer?
โProgramming Languages.
โข How to make the computer carryout the
instructions efficiently?
โCompilers.
cs480(Prasad) L2Syntax 2
Anatomy of a Compiler
Intermediate Code Optimizer
Code Generator
Optimized Intermediate Representation
Assembly code
Intermediate Code Generator
Intermediate Representation
Lexical Analyzer (Scanner)
Syntax Analyzer (Parser)
Token Stream
Parse Tree
Program (character stream)
cs480(Prasad) L2Syntax 3
What is a Lexical Analyzer?
โข Examples of Token
โข Operators = + - > ( { := == <>
โขKeywords if while for int double
โข Numeric literals 43 6.035 -3.6e10 0x13F3A
โข Character literals โaโ โ~โ โ\โโ
โข String literals โ3.142โ โFallโ โ\โ\โ = emptyโ
โข Examples of non-token
โข White space space(โ โ) tab(โ\tโ) end-of-line(โ\nโ)
โข Comments /*this is not a token*/
Source program text Tokens
cs480(Prasad) L2Syntax 4
Lexical Analyzer in Action
โข Partition input program text into sequence of
tokens, attaching corresponding attributes.
โ E.g., C-lexeme โ015โ token NUM attribute 13
โข Eliminate white space and comments
f
for
o r v a r 1 = 1 0 v a r 1 < =
ID(โvar1โ) eq_op Num(10) ID(โvar1โ) leq_op