Skip to content
Newer
Older
100755 109 lines (89 sloc) 2.74 KB
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
1 %{
2 my @nonTerms = ();
3 %}
4
0e17145 @ulisses a nivel de gramaticas estamos a progredir... Yapp a reconhecer Yacc
authored Mar 20, 2011
5 %token STRING_TERMINAL STRING_NON_TERMINAL SEPARATOR
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
6
7 %%
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
8 productions : production { return $_[1]; }
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
9 | productions production {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
10 push @{$_[1]},@{$_[2]};
11 return $_[1];
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
12 }
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
13
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
14 ;
15
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
16 production : nonTerminal ':' derivs ';' { return [ { $_[1] => $_[3] } ]; }
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
17 ;
18
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
19 derivs : nts { return [$_[1]]; }
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
20 | derivs '|' nts {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
21 push @{$_[1]},$_[3];
22 return $_[1];
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
23 }
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
24 | '|' nts { return [$_[2], undef]; }
25 | '|' { return [undef]; }
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
26 ;
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
27 nts : nt { return $_[1]; }
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
28 | nts nt {
d046c4d @ulisses para el Hugo
authored Mar 20, 2011
29 push @{$_[1]},@{$_[2]};
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
30 return $_[1];
31 }
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
32 ;
33 nt : terminal {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
34 return [ { $_[1] => 'terminal' } ];
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
35 }
36 | nonTerminal {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
37 return [ { $_[1] => 'nonTerminal' } ];
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
38 }
39 | sep {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
40 return [ { $_[1] => 'sep' } ];
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
41 }
42 ;
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
43 terminal : STRING_TERMINAL {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
44 return $_[1];
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
45 }
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
46 ;
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
47 nonTerminal : STRING_NON_TERMINAL {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
48 return $_[1];
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
49 }
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
50 ;
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
51 sep : SEPARATOR {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
52 return $_[1];
1142b6f @ulisses gemini a ajudar :)
authored Mar 20, 2011
53 }
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
54 ;
55
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
56 %%
57 package main;
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
58 use Data::Dumper;
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
59
60 our($debug);
61
ad54bed @ulisses correccao na gramatica de gramaticas Yacc => BugFix
authored Mar 20, 2011
62 #$debug = 1;
0e17145 @ulisses a nivel de gramaticas estamos a progredir... Yapp a reconhecer Yacc
authored Mar 20, 2011
63
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
64 my $parser=new YaccToANTLR();
65 my $text = init_lex();
66
67 if($debug){testlex(); exit 0;}
68
69 my $value=$parser->YYParse(yylex => \&lex, yyerror => \&yyerror);
70
71 sub yyerror {
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
72 print STDERR "Syntax Error:";
73 if ($_[0]->YYCurtok) {
74 printf STDERR (' a "%s" (%s) was found where "%s" was expected'."\n",
75 $_[0]->YYCurtok, $_[0]->YYCurval, $_[0]->YYExpect)
76 }
77 else{ print STDERR "Expecting one of ",join(", ",$_[0]->YYExpect),"\n";}
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
78 }
79
80 sub testlex{
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
81 my ($t,$e)=lex();
82 while($t){
83 print "T=$t V=$e\n";
84 ($t,$e)=lex();
85 }
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
86 }
87
88 sub init_lex{ join("", <>) ; }
89
0e17145 @ulisses a nivel de gramaticas estamos a progredir... Yapp a reconhecer Yacc
authored Mar 20, 2011
90 sub lex {
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
91 for($text){
92 s/^[ \t\n\r]+// ;
93 s/^([A-Z_]\w*[a-z_])// and return ("STRING_NON_TERMINAL", $1);
94 s/^([A-Z]+)// and return ("STRING_TERMINAL", $1);
95 s/^([:;|])// and return ($1, $1);
96 s/^(['"]\W['"])// and return ("SEPARATOR", $1);
97
98 return ('',undef);
99 print STDERR "Erro: unknown char <$_>\n";
100 }
bc4b4ee @ulisses adicionado o conversor de gramaticas YAcc para ANTLR
authored Mar 19, 2011
101 }
102
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
103 sub main {
35277f2 @ulisses Gramatica Yapp a despejar uma estrutura (Obrigado Hugo)
authored Mar 20, 2011
104 print Dumper $value;
5a84c7a @ulisses yacc to antlr
authored Mar 20, 2011
105 }
106
107 main();
108
Something went wrong with that request. Please try again.