2
Scanner (Lexical
Analysis)
Parser and Semantic
Checks
Regular Expressions +
State (flex, jflex, ...)
Context-free grammars
+ actions/attributes
(yacc, bison, Polyglot,
JastAdd, ...)
3
Lexical specification for
original language
Grammar and actions for
original language
Modified lexical specification
for extended language
Grammar and actions for
original language
Grammar
rules for
extension
4
Lexical
rules for
extension
Lexical specification for
original language
Lexical specification for
original language
Grammar and actions for
original language
Grammar and actions for
original language
Grammar
rules for
extension
5
• Java + HTML
• Java + Aspects (AspectJ)
• Java + SQL
• MATLAB + Aspects (AspectMatlab)
6
• Nested C-style comments
• Javadoc comments
• Floating-point constants
• URL
• regular expressions
•…
7
8
9
• programmer specifies regular
expressions + actions
• tools generate a finite automatonbased implementation
• states are used to handle different
language contexts
10
1
2
3
4
5
6
7
8
9
10
11
12
%%
%class L ex er
I dent i¯er = [: j l et t er :] [: j let t er digit :]¤
...
%st at e ST R I N G
%%
< Y Y INIT IA L> f
" abst r act "
f r et ur n sy mb ol( sy m .A B ST R A C T ) ; g
f I dent i¯er g f r et ur n sy mb ol( sy m .I D EN T I F I ER ) ; g
n"
f st r ing . set L engt h( 0) ; y y b egin( ST R I N G ) ; g
...
g
13
14
15
16
17
18
19
< ST R I N G > f
n"
f y y b egin( Y Y I N I T I A L ) ; r et ur n ...; g
[^ nnnr n" nn]+ f st r ing . app end( y y t ex t ( ) ) ; g
nnt
f st r ing . app end( ' nt ' ) ; g
...
g
11
12
Specification
in one file.
13
Each
component
specified in
its own file.
Layout
specified in its
own file.
14
15
16
17
1
2
3
# som e pr op er t i es
nam e= pr op er t ies
dat e= 2009/ 09/ 21
4
5
6
# som e m or e pr op er t ies
ow ner = r oot
Properties
Key
Value
Util_Patterns
18
1
2
%com p onent ut il pat t er ns
%help er
3
4
5
6
7
lineTer m inat or = [nr nn] j " nr nn"
ot her W hit espace = [ nt nfnb]
ident i¯er = [a¡ zA ¡ Z][a¡ zA ¡ Z0¡ 9 ]¤
com m ent = # [^ nr nn]¤
19
1
2
3
4
%com p onent key
%ex t er n " Token sy mb ol ( int ) "
%ex t er n " Token sy mb ol ( int , St r i ng) "
%ex t er n " voi d er r or ( St r ing) t hr ow s L ex er Ex cept ion"
5
6
%%
7
8
9
10
11
12
13
14
15
16
17
%%inher it ut il pat t er ns
f lineTer m i nat or g f : / ¤ignor e¤/ :g
f ot her W hit espaceg f : / ¤ignor e¤/ :g
" = " f : r et ur n sy mb ol ( A SSI G N ) ; :g A SSI G N
%:
f i dent i ¯er g f : r et ur n sy mb ol( K EY , y y t ex t ( ) ) ; :g
f com m ent g f : / ¤ignor e¤/ :g
%:
< < A N Y > > f : er r or ( " U nex p ect ed char ' " + y y t ex t ( ) + " ' " ) ; :g
< < EOF > > f : r et ur n sy mb ol ( EOF ) ; :g
20
1
2
3
4
5
6
%com p onent value
%ex t er n " Token sy mb ol ( int , St r ing, int , int , int , int ) "
%app endf
r et ur n sy mb ol( VA L U E, t ex t , st ar t L ine, st ar t C ol,
endL ine, endC ol) ;
%app endg
7
8
%%
9
10
11
12
13
14
15
%%inher it ut il pat t er ns
f l ineTer m inat or g f : :g L I N E T ER M I N A T OR
%:
%:
< < A N Y > > f : app end( y y t ex t ( ) ) ; :g
< < EOF > > f : :g L I N E T ER M I N AT OR
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package pr op er t ies;
%%
im p or t st at ic pr op er t ies. TokenT y p es.¤;
%%
%layout pr op er t ies
%opt ion public " %public"
...
%lex t hr ow " L ex er Ex cept ion"
%com p onent key
%com p onent value
%st ar t key
%%
%%emb ed
%nam e key val ue
%host key
%guest value
%st ar t A SSI G N
%end L I N E T ER M I N A T OR
22
properties.mll
util_patterns.mlc
MetaLexer
properties.jflex
key.mlc
value.mlc
23
24
25
26
27
28
Woven output
New Component adds
some rules and inherits
original component.
Original Component
O
29
30
AspectJ and Extensions
1
2
3
4
5
6
7
8
%%emb ed
%nam e p er clause
%host asp ect decl
%guest p oi nt cut
%st ar t [P ER C F L OW P ER C F L OW B EL OW P ERTA R G ET
P ERT H I S] L PA R EN
%end R PA R EN
%pai r L PA R EN , R PA R EN
9
10
11
12
13
14
15
%%emb ed
%nam e p oint cut
%host j ava, asp ect
%guest p oi nt cut
%st ar t P OI N T C U T
%end SEM I C OL ON
31
PLDI 2011 Tutorial on McLab!!!!!
32
33
34
35
Descargar

Structure of a Compiler Front-End