95 3002 Se m an 95 3002 tic

  • Slides: 17
Download presentation
95. 3002 Se m an 95: 3002 tic Ro uti ne s Wilf La.

95. 3002 Se m an 95: 3002 tic Ro uti ne s Wilf La. Londe, @ 1998, 1999, 2000

Semantic Actions • Routines that aid the implementation of scanners and parsers. Nothing special.

Semantic Actions • Routines that aid the implementation of scanners and parsers. Nothing special. • If they are general purpose, they should be in the scanner/parser. • If they only apply to a specific programming language, they should be in the sponsor. 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Invoking Routines Stored As Data • Smalltalk, Ruby, Java have meta-level facilities that allow

Invoking Routines Stored As Data • Smalltalk, Ruby, Java have meta-level facilities that allow you to run such routines. • C++ does not. So you have to use a big switch or a series of hardwired if statements. Because of this, some things absolutely cannot be done in C++; e. g. an object filer. Given a precompiled object filer, followed by the addition of a class that was not there before, it is possible to file out an instance of the class and to file it back in. 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Invoking Routines In Smalltalk 10 factorial 10 perform: #factorial 10 + 20 10 perform:

Invoking Routines In Smalltalk 10 factorial 10 perform: #factorial 10 + 20 10 perform: #'+' with: 20 10 between: 5 and: 30 10 perform: #between: and: with: 5 with: 30 'abcd' perform: #replace. From: to: with: starting. At: with: 2 with: 3 with: '@hi' with: 2 'abcd' replace. From: 2 to: 3 with: '@hi' starting. At: 2 Or more generallly 10 perform: #factorial with. Arguments: #() 10 perform: #'+' with. Arguments: #(20) 10 perform: #between: and: with. Arguments: #(5 30) 'abcd' perform: #replace. From: to: with: starting. At: with. Arguments: #(2 3 '@hi' 2) 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Implementing run for Semantic Tables in Smalltalk Remember: 4 instance variables in semantic tables

Implementing run for Semantic Tables in Smalltalk Remember: 4 instance variables in semantic tables transducer action parameters goto run | recipient : = (transducer class can. Understand: action) if. True: [transducer] if. False: [transducer sponsor]. recipient perform: action with. Arguments: parameters. ^goto "Next table after this one…" 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Built in Semantic Actions In a parser build. Tree: root. Node | children |

Built in Semantic Actions In a parser build. Tree: root. Node | children | "Pick up the children from the tree stack between left and right inclusive (provided they're not nil) and build a tree with the given label. Store it in instance variable new. Tree so a reduce table can use it. " children : = (left to: right) collect: [: index | tree. Stack at: index] when: [: index | (tree. Stack at: index) not. Nil]. new. Tree : = Tree new label: root. Node; children: children. 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Built in Semantic Actions In a parser build. Tree. From. Index: index "Index is

Built in Semantic Actions In a parser build. Tree. From. Index: index "Index is positive (1, 2, 3, . . . ) => label is in the token relative to the left end; i. e. , to the right of left end. " "index is negative (-1, -2, -3. . . ) => label is in the token relative to the right end; i. e. , to the left of right end. " | children : = (left to: right) collect: [: index | tree. Stack at: index] when: [: index | (tree. Stack at: index) not. Nil]. new. Tree : = Tree new label: ( index is. Positive if. True: [token. Stack at: left + index - 1] f. False: [token. Stack at: right + index + 1]) symbol children: children. 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Built in Semantic Actions In a scanner build. Token: label "Create a token with

Built in Semantic Actions In a scanner build. Token: label "Create a token with the supplied label and the characters in kept. Characters and put the result in token so peek can return it when requested. Reset kept. Characters to an empty string so the process can repeat. “ token : = Token new label: label as. Symbol; symbol: kept. Characters as. Symbol. kept. Characters : = ''. 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Notation • Use the transduction notation “Þ #Name”. build. Tree: #Name To mean In

Notation • Use the transduction notation “Þ #Name”. build. Tree: #Name To mean In a parser grammar build. Token: #Name In a scanner grammar 95: 302 Wilf La. Londe @1998, 1999, 2000

95. 3002 An E Se xam ma p nti le o c. A f.

95. 3002 An E Se xam ma p nti le o c. A f. S Sc an ctio peci ne ns a rs for l 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

Describing a String in a Scanner Grammar non. Quote. Keep $'Keep Quoted. String ®

Describing a String in a Scanner Grammar non. Quote. Keep $'Keep Quoted. String ® $'No. Keep 1 $'No. Keep 2 3 Þ #String {end. Of. File} 4 #missing. Quote 5 An error message semantic action 95: 302 Wilf La. Londe @1998, 1999, 2000

Making the Semantic Action Explicit • Precede semantic actions by appropriate lookahead (but not

Making the Semantic Action Explicit • Precede semantic actions by appropriate lookahead (but not twice) non. Quote. Keep $'Keep Quoted. String ® $'No. Keep 1 2 $'No. Keep {end. Of. File} 5 3 {end. Of. File, non. Quote} #emit. Token (#String) 4 #missing. Quote 95: 302 6 Wilf La. Londe @1998, 1999, 2000

Handling Unprintable Characters unknown = 0. . 255 – printables. Unrecognizeable. Character ® 1

Handling Unprintable Characters unknown = 0. . 255 – printables. Unrecognizeable. Character ® 1 unknown. No. Keep 2 #unknown. Character 3 95: 302 Wilf La. Londe @1998, 1999, 2000

Handling Unrecognizeable Characters unknown. No. Keep non. Quote. Keep 4 #unknown. Character $'Keep $'No.

Handling Unrecognizeable Characters unknown. No. Keep non. Quote. Keep 4 #unknown. Character $'Keep $'No. Keep Quoted. String ® 2 $'No. Keep {end. Of. File} 1 3 {end. Of. File, non. Quote, unknown} 6 #emit. Token (#String) 7 5 #missing. Quote 95: 302 Wilf La. Londe @1998, 1999, 2000

In Regular Expression Notation Inside = non. Quote. Keep | unknown. No. Keep #unknown.

In Regular Expression Notation Inside = non. Quote. Keep | unknown. No. Keep #unknown. Character | $'No. Keep $'Keep Quoted. String ® $'No. Keep Inside* $'No. Keep {end. Of. File, non. Quote, unknown} #String | more convenient notation No. Keep than emit. Token (…) $' Inside* {end. Of. File} #missing. Quote #String 95: 302 Wilf La. Londe @1998, 1999, 2000

95. 3002 An E Se xam ma p nti le o c. A f.

95. 3002 An E Se xam ma p nti le o c. A f. S Pa cti pe rse on cia rs s fo l r 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000

For UI That Format Your Code Statement ® "while" "(" Expression ")" "{" #indent

For UI That Format Your Code Statement ® "while" "(" Expression ")" "{" #indent #newline Statement * "}" "; " #exdent #newline Similar for “repeat loops”, ”for loops”, “if statements”, etc. 95: 3002 Wilf La. Londe, @ 1998, 1999, 2000