Efi Arazi School of Computer Science
Efi Arazi School of Computer Science
Efi Arazi School of Computer Science
Compiler I:
Syntax Analysis
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 1
Course map
Assembly
Language
Assembler
Chapter 6
abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 2
Back-end:
from the
intermediate Intermediate code
language to
binary code. VM
implementation VM imp. VM imp.
VM over the Hack VM
over CISC over RISC
emulator platform lectures
platforms platforms
... ...
CISC RISC other digital platforms, each equipped Any Hack
machine machine with its VM implementation computer computer
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 3
Some
Some Other
Jack
compiler compiler
compiler
Intermediate code
VM
implementation VM imp. VM imp.
VM over the Hack
over CISC over RISC
emulator platform
platforms platforms
Syntax Analyzer
Jack Toke-
Code (Chapter 11) VM
Parser Gene
Program nizer code
-ration
Front-end:
Syntax analysis: understanding the semantics implied by the source code
Tokenizing: creating a list of “atoms”
Parsing: matching the atom list with the language grammar
XML output = proof that the syntax analyzer is parsing correctly
Code generation: reconstructing the semantics using the target syntax.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 4
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 5
Jack Tokenizer
Source code
if
if (x
(x << 153)
153) {let
{let city
city == ”Paris”;}
”Paris”;}
Tokenizer’s output
<tokens>
<tokens>
<keyword>
<keyword> if
if </keyword>
</keyword>
<symbol>
<symbol> (( </symbol>
</symbol>
<identifier>
<identifier> xx </identifier>
</identifier>
<symbol>
<symbol> <
< </symbol>
</symbol>
<integerConstant>
<integerConstant> 153153 </integerConstant>
</integerConstant>
<symbol>
<symbol> )) </symbol>
</symbol>
<symbol>
<symbol> {{ </symbol>
</symbol>
<keyword>
<keyword> let
let </keyword>
</keyword>
<identifier>
<identifier> city
city </identifier>
</identifier>
<symbol>
<symbol> == </symbol>
</symbol>
<stringConstant>
<stringConstant> Paris
Paris </stringConstant>
</stringConstant>
<symbol>
<symbol> ;; </symbol>
</symbol>
<symbol>
<symbol> }} </symbol>
</symbol>
</tokens>
</tokens>
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 6
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 7
Examples
context free context dependent
- parse 1 parse 2
sqrt
* discussed with
discussed
+ 2 *
she sex her doctor
5 3 9 4 she with
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 8
simpleIf:
simpleIf: 'if'
'if' '('
'(' expression
expression ')'
')' statement
statement
ifElse:
ifElse: 'if'
'if' '('
'(' expression
expression ')'
')' statement
statement 'else'
'else' statement
statement
statementSequence:
statementSequence: '' ////null,
'' null,i.e.
i.e.the
theempty
emptysequence
sequence
|| statement
statement ';' ';' statementSequence
statementSequence
code sample
expression:
expression: ////definition
definitionofofan
anexpression
expressioncomes
comeshere
here if
if (some
(some expression)
expression) {{
statement;
statement;
////more
moredefinitions
definitionsfollow
follow while
while (some
(some expression)
expression)
statement;
statement;
statement;
statement;
Simple (terminal) forms / complex (non-terminal) forms }}
if
if (some
(some expression)
Grammar = set of rules on how to construct if
expression)
if (some
(some expression)
expression)
complex forms from simpler forms some
some statement;
statement;
}}
Highly recursive.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 9
while
(
count
<=
100 expression statement
)
{
count
++ statementSequence
;
...
statement statementSequence
code sample
while
while (some
(some expression)
expression) {{
some
some statement;
statement;
some
some statement;
statement;
while
while (some
(some expression)
expression) {{
while
while (some
(some expression)
expression)
some
some statement;
statement;
some
some statement;
statement;
}}
}}
Parsing:
(Steven Pinker,
The Language Instinct)
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 12
’x’: xxappears
’x’: appearsverbatim
verbatim
x: xxisisaalanguage
x: languageconstruct
construct
x?: xxappears
x?: appears00oror11times
times
x*: xxappears
x*: appears00orormore
moretimes
times
x|y: either
x|y: eitherxxor
oryyappears
appears
(x,y): xxappears,
(x,y): appears,then
theny.y.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 13
’x’: xxappears
’x’: appearsverbatim
verbatim
x: x isaalanguage
x: x is languageconstruct
construct
x?: xxappears
x?: appears00oror11times
times
x*: x appears 0 ormore
x*: x appears 0 or moretimes
times
x|y: either
x|y: eitherxxor
oryyappears
appears
(x,y): xxappears,
(x,y): appears,then
theny.y.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 14
Syntax Analyzer
Jack Toke-
Code (Chapter 11) VM
Parser Gene
Program nizer code
-ration
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 16
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 10: Compiler I: Syntax Analysis slide 17