Java Reference
In-Depth Information
1 Prog
Dcls Stmts $
2 Dcls
Dcl Dcls
3
| λ
4 Dcl
floatdcl
id
5
|
intdcl
id
6 Stmts
Stmt Stmts
7
| λ
8 Stmt
id assign Val Expr
9
|
print
id
10 Expr
plus Val Expr
11
|
minus Val Expr
12
| λ
13 Val
id
14
|
inum
15
|
fnum
Figure 2.1: Context-free grammar for ac.
2.2 Formal Definition of ac
Before translating ac to dc we must first understand the syntax and semantics
of the ac language. The informal definitions above may generally describe ac,
but they are too vague to serve as a formal definition. We therefore follow
the example of most programming languages and use a context-free gram-
mar (CFG) to specify our language's syntax and regular expressions to specify
the basic symbols of the language.
2.2.1 Syntax Specification
While CFGs are discussed in detail in Chapter 4, we presently view a CFG
simply as a set of productions or rewriting rules .ACFGfortheac language
is given in Figure 2.1. To improve readability, multiple productions for the
same symbol can be specified using an arrow for the first production and bar
symbols to separate the rest of the productions. For example, Stmt serves the
same role in each of the productions:
Stmt
id assign Val Expr
|
print
id
These productions indicate that a Stmt can be replaced by one of two strings
of symbols.
In the first rule, Stmt is rewritten by symbols that represent
 
 
 
Search WWH ::




Custom Search