An Attribute Grammar for Tiny
-
Upload
yeo-fernandez -
Category
Documents
-
view
57 -
download
0
description
Transcript of An Attribute Grammar for Tiny
![Page 1: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/1.jpg)
An Attribute Grammar for Tiny
Prepared by
Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida
Programming Language PrinciplesLecture 18
![Page 2: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/2.jpg)
An 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"
![Page 3: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/3.jpg)
An Attribute Grammar for Tiny (cont’d)
• equality operator "="• binary operators "+" and "-"• unary "-", "not".
• Assignment statements.• Variables must be assigned a value
before they are used.• No declarations.
![Page 4: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/4.jpg)
An Attribute Grammar for Tiny (cont’d)
• Statements:• if-then-else• while• statement sequencing (";")• output statement.
![Page 5: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/5.jpg)
Tiny’s Syntax
Tiny → 'program' Name ':' S 'end' Name '.’ => 'program';S → 'assign' Name ':=' Exp => 'assign' → 'output' Exp => 'output' → 'if' Exp 'then' S 'else' S 'fi' => 'if' → 'while' Exp 'do' S 'od' => 'while' → S list ';' => ';' ;Exp → Term '=' Term => '=' → Term;Term → Term '+' Factor => '+' → Term '-' Factor => '-' → Factor;
![Page 6: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/6.jpg)
Tiny’s Syntax (cont’d)
Factor → '-' Factor => '-' → 'not' Factor => 'not'
→ Name
→ 'read' => 'read'
→ '<integer>'
→ '(' Expression ')';
Name → '<identifier>';
![Page 7: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/7.jpg)
Sample Tiny Program
• Copies 10 integers from input to output:
program copy: assign i := 1; while not (i=11) do output read; assign i := i + 1 odend copy.
![Page 8: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/8.jpg)
AST Grammar for Tiny
P → <'program' '<identifier>' E '<identifier>'> E → <'assign' '<identifier>' E> → <'output' E> → <'if' E E E> → <'while' E E> → <';' E+> → <'not' E> → <'=' E E> → <'+' E E> → <'-' E E> → <'-' E> → '<identifier>' → '<integer>' → 'read'
![Page 9: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/9.jpg)
Note
• 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.
![Page 10: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/10.jpg)
Target Machine Instruction Set
PC := 1;
Next:
case Code[PC] of
save n:stack[n] := stack[top--]
load n:stack[++top] := stack[n]
negate:stack[top] := -stack[top]
not: stack[top] := not stack[top]
add: t := stack[top--]; stack[top] := stack[top] + t
subtract: t := stack[top--]; stack[top] := stack[top] - t
equal: t := stack[top--]; stack[top] := stack[top] = t
![Page 11: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/11.jpg)
Target Machine Instruction Set (cont’d)
read: stack[++top] := getinteger(input)
print: putinteger(stack[top--])
lit n: stack[++top] := n
goto n: PC := n; goto Next
iffalse n: if stack[top--] = 0 then PC:=n; goto Next fi
iftrue n: if stack[top--] = 1 then PC:=n; goto Next fi
stop: halt
end;
++PC;
goto Next;
![Page 12: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/12.jpg)
Target Code for our Sample Tiny Program
1: lit 1 # i := 12: load 1 # i = 113: lit 114: equal5: not6: iffalse 14 # while7: read8: print9: load 1 # i := i + 110: lit 111: add12: save 113: goto 2 # od14: stop # end
program copy:
assign i := 1;
while not (i=11) do
output read;
assign i := i + 1
od
end copy.
![Page 13: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/13.jpg)
Semantic Errors
• Variables used before being initialized.• Non-boolean expression in "while", "not",
or "if".• Non-integer expression in "=", "-", or "+".• Program names do not match.
![Page 14: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/14.jpg)
Data 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.
![Page 15: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/15.jpg)
Data Structures Required (cont’d)
• 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.
![Page 16: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/16.jpg)
Attributes
• 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.
![Page 17: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/17.jpg)
Attributes (cont’d)
• ALL attributes are both synthesized and inherited.
• Convention:• a is the synthesized attribute a.• a is the inherited attribute a.
![Page 18: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/18.jpg)
Synthesized 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".
![Page 19: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/19.jpg)
Synthesized 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 .
![Page 20: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/20.jpg)
Tree 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 < i <= n•a () = a (n)
![Page 21: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/21.jpg)
Tiny's Attribute Grammar
E → '<integer>:n'
code () = gen (code (), "lit", "n")next () = next () + 1top () = top () + 1type () = "integer"
![Page 22: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/22.jpg)
![Page 23: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/23.jpg)
Tiny's Attribute Grammar (cont’d)
E → '<identifier:x>'
code () = gen (code (), "load", lookup("x"))
next () = next () + 1top () = top () + 1type () = "integer"error () = if lookup("x") = 0 then gen (error (), "identifier un-initialized")
else error ()
![Page 24: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/24.jpg)
![Page 25: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/25.jpg)
Tiny's Attribute Grammar (cont’d)
E → read
code () = gen (code (), "read")next () = next () + 1top () = top () + 1type () = "integer"
![Page 26: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/26.jpg)
![Page 27: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/27.jpg)
Tiny's Attribute Grammar (cont’d)
E → < - E >
code () = gen (code (1), "negate")next () = next (1) + 1type () = "integer"error () = if type (1) = "integer" then error (1) else gen (error (1), “Illegal type for minus"))
![Page 28: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/28.jpg)
![Page 29: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/29.jpg)
Tiny's Attribute Grammar (cont’d)
E → <not E >
code () = gen (code (1), “not")next () = next (1) + 1type () = “boolean"error () = if type (1) = “boolean" then error (1) else gen (error (1), "Illegal type for not")
![Page 30: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/30.jpg)
![Page 31: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/31.jpg)
Tiny's Attribute Grammar (cont’d)
E → < + E E >
code () = gen (code (2), "add")next () = next (2) + 1top () = top (2) - 1type () = "integer"error () = if type (1)=type (2)= "integer" then error (2) else gen (error (2), "Illegal type for plus")
![Page 32: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/32.jpg)
![Page 33: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/33.jpg)
Tiny's Attribute Grammar (cont’d)
E → < - E E >
code () = gen (code (2), “subtract")next () = next (2) + 1top () = top (2) - 1type () = "integer"error () = if type (1)=type (2)= "integer" then error (2) else gen (error (2), "Illegal type for minus")
![Page 34: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/34.jpg)
![Page 35: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/35.jpg)
Tiny's Attribute Grammar (cont’d)
E → < = E E >
code () = gen (code (2), “equal")next () = next (2) + 1type () = “boolean"top () = top (2) - 1error () = if type (1) = type (2) then error (2) else gen (error (2), "Type clash in equal comparison")
![Page 36: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/36.jpg)
![Page 37: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/37.jpg)
Tiny's Attribute Grammar (cont’d)
E → < output E >
code () = gen (code (1), “print")next () = next (1) + 1top () = top (1) - 1type () = “statement"
error () = if type (1) = "integer" then error (1) else gen (error (1), "Illegal type for output")
![Page 38: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/38.jpg)
![Page 39: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/39.jpg)
Tiny's Attribute Grammar (cont’d)
E → < assign '<identifier:x>' E >
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
![Page 40: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/40.jpg)
E → < assign '<identifier:x>' E >(cont’d)top () = if lookup ("x") = 0
then top (2) else top (2) - 1error () = if type (2) = "integer" then error (2)
else gen (error (2), "Assignment type clash")
type () = "statement"
![Page 41: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/41.jpg)
![Page 42: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/42.jpg)
Tiny's Attribute Grammar (cont’d)
E -> < ; E* >
Use Defaults !
![Page 43: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/43.jpg)
Tiny's Attribute Grammar (cont’d)
E → < if E E E >
code (2) = gen (code (1),"iffalse", next (2) + 1)
next (2) = next (1) + 1top (2) = top (1) - 1code (3) = gen (code (2), "goto", next (3))next (3) = next (2) + 1error (2) = if type (1) = "boolean" then error (1) else gen (error (1), "Illegal expression for if")
![Page 44: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/44.jpg)
E → < if E E E > (cont’d)
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")
![Page 45: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/45.jpg)
![Page 46: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/46.jpg)
Tiny's Attribute Grammar (cont’d)
E → < while E E >
code (2) = gen (code (1), "iffalse", next (2) + 1)next (2) = next (1) + 1top (2) = top (1) - 1code () = gen (code (2), "goto", next ())next () = next (2) + 1type () = "statement"
![Page 47: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/47.jpg)
E → < while E E > (cont’d)
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")
![Page 48: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/48.jpg)
![Page 49: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/49.jpg)
Tiny's Attribute Grammar (cont’d)
Tiny → < program '<identifier:x>' E '<identifier:y>' >
code (2) = Openerror (2) = Opennext (2) = 1top (2) = 0code () = close (gen (code (2), "stop"))error () = close (if x = y then error (2) else gen (error (2), "program names don't match") )
![Page 50: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/50.jpg)
![Page 51: An Attribute Grammar for Tiny](https://reader035.fdocuments.net/reader035/viewer/2022062314/568135d1550346895d9d3692/html5/thumbnails/51.jpg)
An Attribute Grammar for Tiny
Prepared by
Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida
Programming Language PrinciplesLecture 18