Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a metagrammar?

Tags:

grammar

I'm currently learning syntactic analysis. I'm trying to make a metagrammar that could generate that particular grammar:

A ⇒ A '+' C | C ;  
C ⇒ C * Q ;  
C ⇒ Q ;  
Q ⇒ a | b | 'A' | "B" | "(" A ")" | <num> ;  
<num> ⇒ <Signed Int> | Float ;  
<Signed Int> ⇒ Signe Int ;  
Signe ⇒ '-' | '+' | ~eps~  
<Int> ⇒ Digit Int | Digit ;  
Digit ⇒ '1' | '2' | '3' | '4' | 5 | 6 | 7 | 8 | "9" | "0" ;  
Float ⇒ Int '.' Int ;  

Where <> are ignored (i.e. <int> is the same as int), single/double quotes are for a string, ~eps~ is for epsilon. Everything else is considered a symbol (whether it is terminal or nonterminal).

Currently I have something like this:

S ⇒ left "⇒" right ";" | ε  
left ⇒ symb | "<"symb">"  
right ⇒ QP
Q ⇒ symb | """symb""" | "'"symb"'" | "<"symb">" | ε  
P ⇒ symb | '|' Q | ε  

But it feels so wrong to me and I'm not so sure on what do to. Is there a method to determinate a metagrammar? How could I go about this one?

like image 553
goutchia Avatar asked Nov 05 '15 00:11

goutchia


1 Answers

Not a bad start. You really should define symb:

letter = "A" | "B" |  ...  | "Z" ;
symb = letter symb | letter ;

But your metagrammar only allows one grammar rule. To allow multiple rules, I think you want to write:

 S = R S | ε ;
 R = left "⇒" right ";"

You might be very interested in tools that use metagrammars to process themselves and other grammars. This small paper about MetaII, from (ready?) 1964 discusses the issue and shows how to build compilers using it. This is a stunning paper, and it will twist your brain (in a good way!)

When you are done with this paper, you will feel very comfortable with metagrammars. (I learned to build compilers using this back in the early 1970s).

like image 183
Ira Baxter Avatar answered Nov 02 '22 22:11

Ira Baxter