Title: An Attribute Grammar for Tiny
1An Attribute Grammar for Tiny
Programming Language Concepts
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida
2An Attribute Grammar for Tiny
- Tiny a very small Pascal-like language with the
following - Every program is given a name.
- Variables of type integer.
- Expressions composed of
- variables
- integers
- intrinsic function "read"
- boolean operator "not"
3An Attribute Grammar for Tiny (contd)
- equality operator ""
- binary operators "" and "-"
- unary "-", "not".
- Assignment statements.
- Variables must be assigned a value before they
are used. - No declarations.
4An Attribute Grammar for Tiny (contd)
- Statements
- if-then-else
- while
- statement sequencing ("")
- output statement.
5Tinys Syntax
- Tiny ? 'program' Name '' S 'end' Name '.
gt 'program' - S ? 'assign' Name '' Exp gt 'assign'
- ? 'output' Exp gt 'output'
- ? 'if' Exp 'then' S 'else' S 'fi' gt
'if' - ? 'while' Exp 'do' S 'od' gt 'while'
- ? S list '' gt ''
- Exp ? Term '' Term gt ''
- ? Term
- Term ? Term '' Factor gt ''
- ? Term '-' Factor gt '-'
- ? Factor
6Tinys Syntax (contd)
- Factor ? '-' Factor gt '-'
- ? 'not' Factor gt 'not'
- ? Name
- ? 'read' gt 'read'
- ? 'ltintegergt'
- ? '(' Expression ')'
- Name ? 'ltidentifiergt'
7Sample Tiny Program
- Copies 10 integers from input to output
- program copy
- assign i 1
- while not (i11) do
- output read
- assign i i 1
- od
- end copy.
8AST Grammar for Tiny
- P ? lt'program' 'ltidentifiergt' E 'ltidentifiergt'gt
- E ? lt'assign' 'ltidentifiergt' Egt
- ? lt'output' Egt
- ? lt'if' E E Egt
- ? lt'while' E Egt
- ? lt'' Egt
- ? lt'not' Egt
- ? lt'' E Egt
- ? lt'' E Egt
- ? lt'-' E Egt
- ? lt'-' Egt
- ? 'ltidentifiergt'
- ? 'ltintegergt'
- ? 'read'
9Note
- AST grammar simplified.
- No distinction between expressions and
statements. - Our attribute grammar will specify the
translation (compilation) of Tiny programs to
assembly for an abstract machine.
10Target Machine Instruction Set
- PC 1
- Next
- case CodePC of
- save n stackn stacktop--
- load n stacktop stackn
- negate stacktop -stacktop
- not stacktop not stacktop
- add t stacktop-- stacktop
stacktop t - subtract t stacktop-- stacktop
stacktop - t - equal t stacktop-- stacktop
stacktop t -
11Target Machine Instruction Set (contd)
- read stacktop getinteger(input)
- print putinteger(stacktop--)
- lit n stacktop n
- goto n PC n goto Next
- iffalse n if stacktop-- 0 then PCn
goto Next fi - iftrue n if stacktop-- 1 then PCn goto
Next fi - stop halt
- end
- PC
- goto Next
12Target Code for our Sample Tiny Program
program copy assign i 1 while not
(i11) do output read assign i i
1 od end copy.
- 1 lit 1 i 1
- 2 load 1 i 11
- 3 lit 11
- 4 equal
- 5 not
- 6 iffalse 14 while
- 7 read
- 8 print
- 9 load 1 i i 1
- 10 lit 1
- 11 add
- 12 save 1
- 13 goto 2 od
- 14 stop end
13Semantic Errors
- Variables used before being initialized.
- Non-boolean expression in "while", "not", or
"if". - Non-integer expression in "", "-", or "".
- Program names do not match.
14Data Structures Required
- Declaration table.
- Function enter(name,l).
- Binds "name" with stack location "l".
- Returns "l".
- Function lookup(name).
- Returns the location of "name" .
- Returns 0 if "name" is not found.
15Data Structures Required (contd)
- Files.
- Function gen(file, arg1 , ..., argn). Writes a
new line to "file". The line contains arg1 ,
..., argn. Returns the new, modified file. - function Open. Creates a new file.
- function Close. Closes a file.
16Attributes
- code File of code generated.
- next Label of the next instruction
- on the code file.
- error File of semantic errors.
- top Current (predicted) size
- of run-time stack.
- type Type of subtree. Used for
type-checking.
17Attributes (contd)
- ALL attributes are both synthesized and
inherited. - Convention
- a ? is the synthesized attribute a.
- a ? is the inherited attribute a.
18Synthesized and Inherited Attributes
- S(program) code ?, error ?
- I(program)
- S(assign) code ?, next ?, error ?, top ?,
type ? - I(assign) code ?, next ?, error ?, top ?
- All other nodes
- Same synthesized and inherited attributes as
"assign".
19Synthesized and Inherited Attributes
- Most nodes have
- Four inherited attributes (all except type ?).
- All five synthesized attributes.
- One exception
- "program" node
- no inherited attributes.
- two synthesized attributes code ? and error ?.
20Tree Walking Assumptions
- Top-down on the left,
- Bottom-up on the right.
- Defaults
- If axiom is missing, assume
- no kids a ?(?) a ?(?)
- n kids
- a ?(1) a ?(?)
- a ?(i) a ?(i-1), for 1 lt i lt n
- a ?(?) a ?(n)
21Tiny's Attribute Grammar
- E ? 'ltintegergtn'
- code ?(?) gen (code ?(?), "lit", "n")
- next ?(?) next ?(?) 1
- top ?(?) top ?(?) 1
- type ?(?) "integer"
22(No Transcript)
23Tiny's Attribute Grammar (contd)
- E ? 'ltidentifierxgt'
- code ?(?) gen (code ?(?), "load",
lookup("x")) - next ?(?) next ?(?) 1
- top ?(?) top ?(?) 1
- type ?(?) "integer"
- error ?(?) if lookup("x") 0
- then gen (error ?(?),
- "identifier un-initialized")
- else error ?(?)
24(No Transcript)
25Tiny's Attribute Grammar (contd)
- E ? read
- code ?(?) gen (code ?(?), "read")
- next ?(?) next ?(?) 1
- top ?(?) top ?(?) 1
- type ?(?) "integer"
26(No Transcript)
27Tiny's Attribute Grammar (contd)
- E ? lt - E gt
- code ?(?) gen (code ?(1), "negate")
- next ?(?) next ?(1) 1
- type ?(?) "integer"
- error ?(?) if type ?(1) "integer"
- then error ?(1)
- else gen (error ?(1),
- Illegal type for
minus"))
28(No Transcript)
29Tiny's Attribute Grammar (contd)
- E ? ltnot E gt
- code ?(?) gen (code ?(1), not")
- next ?(?) next ?(1) 1
- type ?(?) boolean"
- error ?(?) if type ?(1) boolean"
- then error ?(1)
- else gen (error ?(1),
- "Illegal type for
not")
30(No Transcript)
31Tiny's Attribute Grammar (contd)
- E ? lt E E gt
- code ?(?) gen (code ?(2), "add")
- next ?(?) next ?(2) 1
- top ?(?) top ?(2) - 1
- type ?(?) "integer"
- error ?(?) if type ?(1)type ?(2) "integer"
- then error ?(2)
- else gen (error ?(2),
- "Illegal type for
plus")
32(No Transcript)
33Tiny's Attribute Grammar (contd)
- E ? lt - E E gt
- code ?(?) gen (code ?(2), subtract")
- next ?(?) next ?(2) 1
- top ?(?) top ?(2) - 1
- type ?(?) "integer"
- error ?(?) if type ?(1)type ?(2) "integer"
- then error ?(2)
- else gen (error ?(2),
- "Illegal type for minus")
34(No Transcript)
35Tiny's Attribute Grammar (contd)
- E ? lt E E gt
- code ?(?) gen (code ?(2), equal")
- next ?(?) next ?(2) 1
- type ?(?) boolean"
- top ?(?) top ?(2) - 1
- error ?(?) if type ?(1) type ?(2)
- then error ?(2)
- else gen (error ?(2),
- "Type clash in equal comparison")
36(No Transcript)
37Tiny's Attribute Grammar (contd)
- E ? lt output E gt
- code ?(?) gen (code ?(1), print")
- next ?(?) next ?(1) 1
- top ?(?) top ?(1) - 1
- type ?(?) statement"
- error ?(?) if type ?(1) "integer"
- then error ?(1)
- else gen (error ?(1),
- "Illegal type for output")
38(No Transcript)
39Tiny's Attribute Grammar (contd)
- E ? lt assign 'ltidentifierxgt' E gt
- code ?(?) if lookup("x") 0
- then enter("x",top ?(2)) code ?(2)
- else gen (code ?(2), "save",
lookup("x")) - next ?(?) if lookup("x") 0
- then next ?(2)
- else next ?(2) 1
40E ? lt assign 'ltidentifierxgt' E gt(contd)
- top ?(?) if lookup ("x") 0
- then top ?(2)
- else top ?(2) - 1
- error ?(?) if type ?(2) "integer"
- then error ?(2)
else gen (error ?(2),
"Assignment type clash") - type ?(?) "statement"
41(No Transcript)
42Tiny's Attribute Grammar (contd)
- E -gt lt E gt
- Use Defaults !
43Tiny's Attribute Grammar (contd)
- E ? lt if E E E gt
- code ?(2) gen (code ?(1),"iffalse", next ?(2)
1) - next ?(2) next ?(1) 1
- top ?(2) top ?(1) - 1
- code ?(3) gen (code ?(2), "goto", next ?(3))
- next ?(3) next ?(2) 1
- error ?(2) if type ?(1) "boolean"
- then error ?(1)
- else gen (error ?(1),
- "Illegal expression for if")
44E ? lt if E E E gt (contd)
- error ?(3) if type ?(2) "statement"
- then error ?(2)
- else gen (error ?(2),
- "Statement required for if")
- error ?(?) if type ?(3) "statement"
- then error ?(3)
- else gen (error ?(3),
- "Statement required for if")
45(No Transcript)
46Tiny's Attribute Grammar (contd)
- E ? lt while E E gt
- code ?(2) gen (code ?(1), "iffalse", next ?(2)
1) - next ?(2) next ?(1) 1
- top ?(2) top ?(1) - 1
- code ?(?) gen (code ?(2), "goto", next ?(?))
- next ?(?) next ?(2) 1
- type ?(?) "statement"
47E ? lt while E E gt (contd)
- error ?(2) if type ?(1) "boolean"
- then error ?(1)
- else gen (error ?(1),
- "Illegal expression in while")
- error ?(?) if type ?(2) "statement"
- then error ?(2)
- else gen (error ?(2),
- "Statement required in while")
48(No Transcript)
49Tiny's Attribute Grammar (contd)
- Tiny ? lt program 'ltidentifierxgt' E
'ltidentifierygt' gt - code ?(2) Open
- error ?(2) Open
- next ?(2) 1
- top ?(2) 0
- code ?(?) close (gen (code ?(2), "stop"))
- error ?(?) close (if x y
- then error ?(2)
- else gen (error ?(2),
- "program names don't match")
- )
50(No Transcript)
51An Attribute Grammar for Tiny
Programming Language Concepts
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida