parrot compiler tools -
TRANSCRIPT
![Page 1: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/1.jpg)
Parrot Compiler Tools
Allison RandalThe Perl Foundation &
O'Reilly Media, Inc.
![Page 2: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/2.jpg)
There's an odd misconception in the computing world that writing compilers is hard. This view is fueled by the fact that we don't write compilers very often. People used to think writing CGI code was hard. Well, it is hard, if you do
it in C without any tools.
![Page 3: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/3.jpg)
Parrot VM
Perl 5Perl 6
Ruby Python PHP
![Page 4: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/4.jpg)
Parrot VM
Dylan
Forth
TclPerl 5
SchemePerl 6
Ruby
TeX macro
Python PHP
Objective-CLuaJavascript
Invent your language here.
![Page 5: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/5.jpg)
Parrot VM
PASM (assembly language) PIR (intermediate representation)
Parser Grammar Engine (PGE)
Tree Grammar Engine (TGE)
![Page 6: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/6.jpg)
Parser Grammar Engine
● Regular expressions● Recursive descent● Operator precedence parser
![Page 7: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/7.jpg)
Parser Grammar Engine
● Parsing is recognizing patterns● if # "if" keyword● a == 4 # an expression● then # "then" keyword● print "Hello"; # a statement
● Grammar rules are patterns● rule conditional { ● if <expression> ● then <statement> ● }
![Page 8: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/8.jpg)
Parser Grammar Engine
● PGE is a pattern compiler● grammar 'Simple';● rule ident { [ <alpha> | _ ] \w* }
● Run pgc.pir● $ parrot pgc.pir simple.pg > simple.pir
● PIR output● .sub 'ident'● ... # 308 lines● .end
![Page 9: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/9.jpg)
Parser Grammar Engine
● Use the compiled parser● .sub 'foo'● ...● load_bytecode 'simple.pir'● # retrieve the rule sub● parse = find_global 'Simple', 'ident'●
● source = '_identifier' # the source● match = parse(source) # parse●
● '_dumper'(match) # dump the tree● .end
![Page 10: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/10.jpg)
Parser Grammar Engine
● Rule, token, regex● Rule and token don't backtrack● Rule does smart whitespace matching
● rule conditional { ● if <expression> ● then <statement> ● }● token conditional { ● \s* if \s* <expression> ● \s* then \s* <statement> \s* ● }
![Page 11: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/11.jpg)
Parser Grammar Engine
● Operator precedence parser● proto infix:+ is precedence('=') { ... }● proto infix:- is equiv('infix:+') { ... }●
● proto infix:* is tighter('infix:+') { ... }● proto infix:/ is equiv('infix:*') { ... }
● Associativity● proto infix:= is assoc('right') is
looser('infix:||') { ... }
![Page 12: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/12.jpg)
Tree Grammar Engine
● Attribute Grammars
![Page 13: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/13.jpg)
(Early February, 1967)
Peter [Wegner] asked me what I thought about formal semantics, and I said I liked [Ned] Iron's idea of synthesizingan overall meaning from submeanings. I also said that I liked the way other people had combined Irons's approachwith a top-down or “recursive-descent” parser...
So Peter asked, “Why can't attributes be defined from the top down as well as from the bottom up?”
A shocking idea! Of course I instinctively replied that it wasimpossible to go both bottom-up and top-down. But aftersome discussion I realized that his suggestion wasn't so preposterous after all...
- D. E. Knuth, “The genesis of attribute grammars”
![Page 14: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/14.jpg)
Tree Grammar Engine
● Attribute Grammars● Minimalist Program● Transforming trees
![Page 15: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/15.jpg)
Tree Grammar Engine
● TGE is a transform compiler● grammar ASTGen is TGE::Grammar;●
● transform astout (ident) :language('PIR') {● .local pmc result● result = new 'AST::Ident'● $S2 = node● result.'name'($S2)● ...● .return (result)● }
![Page 16: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/16.jpg)
Tree Grammar Engine
● Run tgc.pir● $ parrot tgc.pir ASTGen.tg > ASTGen.pir
● PIR output● .sub '_ident_astout' :method● .param pmc tree● .param pmc node● ...● .end
![Page 17: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/17.jpg)
Tree Grammar Engine
● Use the compiled transformer● .sub 'foo'● load_bytecode 'ASTGen.pir'● ...● grammar = new 'ASTGen'● astbuilder = grammar.apply(matchtree)● ast = astbuilder.get('astout')●
● ast.dump()● .end
![Page 18: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/18.jpg)
Compiler Tools
● 4 stages● Parse Tree● Abstract Syntax Tree● Opcode Syntax Tree● PIR (or bytecode)
Source
AST
Parse
OST
PIR
![Page 19: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/19.jpg)
Value Transformation
42
![Page 20: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/20.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
token integer { \d+ }
![Page 21: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/21.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
![Page 22: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/22.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
integer
AST treegrammar rule
transform buildast (integer) {...}
![Page 23: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/23.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
integer
AST treegrammar rule
<PAST::Val>value: 42valtype: int
AST node
![Page 24: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/24.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
integer
AST treegrammar rule
<PAST::Val>value: 42valtype: int
AST node
PAST::Val
OST treegrammar rule
transform buildost (PAST::Val) {...}
![Page 25: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/25.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
integer
AST treegrammar rule
<PAST::Val>value: 42valtype: int
AST node
PAST::Val
OST treegrammar rule
<POST::Val>value: 42valtype: int
OST node
![Page 26: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/26.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
integer
AST treegrammar rule
<PAST::Val>value: 42valtype: int
AST node
PAST::Val
OST treegrammar rule
<POST::Val>value: 42valtype: int
OST node
POST::Val
PIR treegrammar rule
transform buildpir (POST::Val) {...}
![Page 27: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/27.jpg)
Value Transformation
42 \d+
Parser grammarrule “integer”
<integer>value: 42
Parse tree
integer
AST treegrammar rule
<PAST::Val>value: 42valtype: int
AST node
PAST::Val
OST treegrammar rule
<POST::Val>value: 42valtype: int
OST node
POST::Val
42
PIR treegrammar rule
![Page 28: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/28.jpg)
Operator Transformation
6 * 9
![Page 29: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/29.jpg)
Operator Transformation
6 * 9 infix:*
Parser grammarOPP rule
proto 'infix:*' is tighter('infix:+') {...}
![Page 30: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/30.jpg)
Operator Transformation
6 * 9 infix:*
Parser grammarOPP rule
<expr>type: 'infix:*'
Parse tree
<integer>value: 6
<integer>value: 9
![Page 31: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/31.jpg)
Operator Transformation
<expr>type: 'infix:*'
Parse tree
<integer>value: 6
<integer>value: 9
![Page 32: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/32.jpg)
Operator Transformation
<expr>type: 'infix:*'
Parse tree
expr
AST treegrammar rule
<integer>value: 6
<integer>value: 9
transform buildast (expr) {...}
![Page 33: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/33.jpg)
Operator Transformation
<expr>type: 'infix:*'
Parse tree
expr
AST treegrammar rule
<integer>value: 6
<integer>value: 9
AST tree
<PAST::Op>op: 'infix:*'
<PAST::Val>value: 6
valtype: int
<PAST::Val>value: 9
valtype: int
![Page 34: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/34.jpg)
Operator TransformationAST tree
<PAST::Op>op: 'infix:*'
<PAST::Val>value: 6
valtype: int
<PAST::Val>value: 9
valtype: int
![Page 35: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/35.jpg)
Operator Transformation
PAST::Op
OST treegrammar rule
AST tree
<PAST::Op>op: 'infix:*'
<PAST::Val>value: 6
valtype: int
<PAST::Val>value: 9
valtype: int
transform buildost (PAST::Op) {...}
![Page 36: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/36.jpg)
Operator Transformation
PAST::Op
OST treegrammar rule
OST tree
<POST::Ops>
<POST::Var>varname: $P2
<POST::Var>varname: $P3
AST tree
<PAST::Op>op: 'infix:*'
<PAST::Val>value: 6
valtype: int
<PAST::Val>value: 9
valtype: int
<POST::Op>op: 'mul'
variablesetup
<POST::Var>varname: $P1
![Page 37: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/37.jpg)
Operator Transformation
● .sub _main :main● new $P1, .Undef● new $P2, .Undef● set $P2, 6● new $P3, .Undef● set $P3, 9● mul $P1, $P2, $P3● .end
![Page 38: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/38.jpg)
Summary
● Attract multiple languages● Easy to use● Simple steps● Hide Complexity● Impossible
![Page 39: Parrot Compiler Tools -](https://reader031.vdocument.in/reader031/viewer/2022020702/61fb1af12e268c58cd5a35b7/html5/thumbnails/39.jpg)
Questions?
● Further Reading– http://parrotcode.org/docs/compiler_tools.html
– Knuth, D. E. (1990) “The genesis of attribute grammars.” Proceedings of the international conference on Attribute grammars and their applications, 1–12.
– Chomsky, Noam (1995). The Minimalist Program. MIT Press.