Documente Academic
Documente Profesional
Documente Cultură
Chapter 1 1 Chapter 1 2
Chapter 1 3 Chapter 1 4
A Derivation (for Figure 1.4)
Concrete Syntax for Wren
<sentence> <noun phrase> <verb phrase> .
<determiner> <noun> <verb phrase> .
Phrase-structure syntax:
the <noun> <verb phrase> .
<program> ::= program <ident> is <block>
the girl <verb phrase> .
the girl <verb> <noun phrase> . <block> ::= <declaration seq>
begin <cmd seq> end
the girl touched <noun phrase> .
<declaration seq> ::=
the girl touched <determiner>
| <declaration> <declaration seq>
<noun> <prep phrase> .
the girl touched the <noun> <prep phrase> . <declaration> ::= var <var list> : <type> ;
the girl touched the cat <prep phrase> . <type> ::= integer | boolean
the girl touched the cat <preposition> <var list> ::= <var> | <var> , <var list>
<noun phrase> . <cmd seq> ::= <cmd> | <cmd> ; <cmd seq>
the girl touched the cat with <noun phrase>
<cmd> ::= <var> := <expr> | skip
.
the girl touched the cat with | read <var> | write <int expr>
<determiner> <noun> . | while <bool expr> do <cmd seq> end while
the girl touched the cat with a <noun> . | if <bool expr> then <cmd seq> end if
the girl touched the cat with a feather .
| if <bool expr> then <cmd seq>
else <cmd seq> end if
Note example of a context-sensitive grammar.
Chapter 1 5 Chapter 1 6
Lexical syntax:
<expr> ::= <int expr> | <bool expr> <relation> ::= <= | < | = | > | >= | <>
<integer expr> ::= <term> <weak op> ::= + | <strong op> ::= * | /
| <int expr> <weak op> <term>
<ident> ::= <letter> | <ident> <letter>
<term> ::= <element> | <ident> <digit>
| <term> <strong op> <element>
<letter> ::= a | b | c | d | e | f | g | h | i | j | k | l | m
<element> ::= <numeral> | <var> |n|o|p|q|r|s|t|u|v|w|x|y|z
| ( <int expr> ) | <element>
<numeral> ::= <digit> | <digit> <numeral>
<bool expr> ::= <bool term>
| <bool expr> or <bool term> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<bool term> ::= <bool element>
| <bool term> and <bool element>
<bool element> ::= true | false | <var> Tokens:
| <comparison>
| not ( <bool expr> ) | ( <bool expr> ) <token> ::= <identifier> | <numeral>
<comparison> ::= <int expr> <relation> <int expr> | <reserved word> | <relation>
| <weak op> | <strong op>
<var> ::= <ident>
| := | ( | ) | , | ; | :
Chapter 1 9 Chapter 1 10
2. All identifiers that appear in a block must be <program> strings derived from
declared in that block. the context-free grammar
May omit some parentheses following the <cmd seq> ::= <command> ( ; <command> )*
precedence rules
Or include command sequences with
Abbreviations commands:
E1 E2 = E1 E2 <command> ::= <command> ; <command>
| skip | read <var> |
E+ = E E*
En = E E E, repeated n times
E? = | E Language Processing:
scan : Character* Token*
Note: E* = | E1 | E2 | E3 | parse : Token* ????
Chapter 1 13 Chapter 1 14
read <variable>
<command> <cmd seq>
Should not be ambiguous. var(m)
;
<command>
A derivation proceeds by replacing one if <bool expr> then <cmd seq> end if
nonterminal at a time by its corresponding write <int expr>
<command>
right-hand side in some production for that <bool term>
<term>
nonterminal.
<bool elem> <variable> := <expr>
<term> <str op> <elem>
A derivation following a BNF definition of a <comparison> var(m) <int expr>
language can be summarized in a derivation <element> * var(m)
(parse) tree. <int expr> <rel> <int expr> <term>
num(22)
<term> = <term> <element>
Although a parser may not produce a
derivation tree, the structure of the tree is <element> <element>
num(0)
embodied in the parsing process.
var(m) - <element>
num(9)
Chapter 1 15 Chapter 1 16
Abstract Syntax
read m; if m=-9 then m:=0 end if; write22*m
Representation of the structure of language
constructs as determined by the parser. Abstract Syntax Tree:
commands
The hierarchical structure of language
constructs can be represented as abstract
syntax trees. read if write
var(m) times
var(m) num(0)
Chapter 1 17 Chapter 1 18
Chapter 1 19 Chapter 1 20
Abstract Syntax
Abstract Syntactic Categories Alternative Abstract Syntax
Program Type Operator
Block Command Numeral Abstract Production Rules
Declaration Expression Identifier Program ::= prog (Identifier, Block)
Abstract Production Rules
Block ::= block (Declaration*, Command+)
Program ::= programIdentifier is Block
Declaration ::= dec (Identifier+, Type)
Block ::= Declaration* beginCommand+ end
Type ::= integer | boolean
Declaration ::= var Identifier+ : Type ;
Command ::= assign (Identifier, Expression)
Type ::= integer| boolean | skip | read (Identifier) | write (Expression)
Command ::= Identifier := Expression | skip | while (Expression, Command+)
| read Identifier | writeExpression | if (Expression, Command+)
| while Expression do Command+ | ifelse (Expression, Command+, Command+)
| if Expression then Command+
| if Expression then Command+ else Command+ Expression ::= Numeral | Identifier | true | false
| not (Expression) | minus (Expression)
Expression ::= Numeral | Identifier | true | false | expr (Operator, Expression, Expression)
| Expression Operator Expression
| Expression | not( Expression) Operator ::= + | | * | / | or | and
| <= | < | = | > | >= | <>
Operator ::= + | | * | / | or | and
| <= | < | = | > | >= | <>
Chapter 1 21 Chapter 1 22
Example
read m;
if m=-9 then m:=0 end if;
write 22*m
[ read (m),
if (expr (equal, ide(m),minus (num(9))),
[ assign (m, num(0)) ]),
write (expr (times, num(22), ide(m)))
]
Chapter 1 23