Download presentation
Presentation is loading. Please wait.
1
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements l Syntax ( 語法 ) vs. Semantics ( 語意 ) »Example: assuming J,K:integer and X,Y:float »I:=J+K vs I:=X+Y l Compilation: 編譯 »matching statements written by the programmer to structures defined by the grammar and generating the appropriate object code
2
2 System Software l Assembler l Loader and Linker l Macro Processor l Compiler l Operating System l Other System Software »RDBS »Text Editors »Interactive Debugging System
3
3 Basic Compiler l Lexical analysis -- scanner »scanning the source statement, recognizing and classifying the various tokens l Syntactic analysis -- parser »recognizing the statement as some language construct l Code generation --
4
4 Scanner PROGRAM STATS VAR SUM, SUMSQ, I SUM := 0 ; SUMSQ := READ ( VALUE ) ;
5
5 Parser l Grammar: a set of rules »Backus-Naur Form (BNF) »Ex: Figure 5.2 l Terminology »Define symbol ::= »Nonterminal symbols <> »Alternative symbols | »Terminal symbols
6
6 Simplified Pascal Grammar
7
7 Parser l ::= READ ( ) l ::= id |,id l ::= id := l ::= | + | - l ::= | * | DIV l ::= id | int | l READ(VALUE) l SUM := 0 l SUM := SUM + VALUE l MEAN := SUM DIV 100
8
8 Syntax Tree
9
9 Syntax Tree for Program 5.1
10
10 Lexical Analysis l Function »scanning the program to be compiled and recognizing the tokens that make up the source statements l Tokens »Tokens can be keywords, operators, identifiers, integers, floating-point numbers, character strings, etc. »Each token is usually represented by some fixed-length code, such as an integer, rather than as a variable-length character string (see Figure 5.5) »Token type, Token specifier (value) (see Figure 5.6)
11
11 Scanner Output l Token specifier »identifier name, integer value l Token coding scheme »Figure 5.5
12
12 Example - Figure 5.6
13
13 Token Recognizer l By grammar » ::= | | » ::= A | B | C | D | … | Z » ::= 0 | 1 | 2 | 3 | … | 9 l By scanner - modeling as finite automata »Figure 5.8(a)
14
14 Recognizing Identifier l Identifiers allowing underscore (_) »Figure 5.8(b)
15
15 Recognizing Integer l Allowing leading zeroes »Figure 5.8(c) l Disallowing leading zeroes »Figure 5.8(d)
16
16 Scanner -- Implementation l Figure 5.10 (a) »Algorithmic code for identifer recognition l Tabular representation of finite automaton for Figure 5.9
17
17 Syntactic Analysis l Recognize source statements as language constructs or build the parse tree for the statements »bottom-up: operator-precedence parsing »top-down:: recursive-descent parsing
18
18 Operator-Precedence Parsing l Operator »any terminal symbol (or any token) l Precedence »* » + »+ « * l Operator-precedence »precedence relations between operators
19
Precedence Matrix for the Fig. 5.2
20
20 Operator-Precedence Parse Example BEGIN READ ( VALUE ) ;
21
(i) … id1 := id2 DIV (ii) … id1 := DIV int - (iii) … id1 := DIV - (iv) … id1 := - id3 * (v) … id1 := - * id4 ;
22
(vi) … id1 := - * ; (vi) … id1 := - ; (vii) … id1 := ;
23
23 Operator-Precedence Parsing l Bottom-up parsing l Generating precedence matrix »Aho et al. (1988)
24
24 Shift-reduce Parsing with Stack l Figure 5.14
25
25 Recursive-Descent Parsing l Each nonterminal symbol in the grammar is associated with a procedure l ::= READ ( ) l ::= | | | l Left recursion » ::= | ; l Modification » ::= {; }
26
26
27
27 Recursive-Descent Parse of READ
28
28 Simplified Pascal Grammar for Recursive- Descent Parser
29
29
30
30
31
31
32
32
33
33
34
34
35
35
36
36
37
37
38
38
39
39
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.