0

I am reading a compiler tutorial here www.buildyourownlisp.com. It uses a parser combinator called mpc. What I have at the moment will parse polish notation, but I'm trying to work out how to use standard notation with it. I just can't seem to how to do it. The rules of the parser are as follows:

.   Any character is required.
a   The character a is required.
[abcdef]    Any character in the set abcdef is required.
[a-f]   Any character in the range a to f is required.
a?  The character a is optional.
a*  Zero or more of the character a are required.
a+  One or more of the character a are required.
^   The start of input is required.
$   The end of input is required.
"ab"    The string ab is required.
'a' The character a is required.
'a' 'b' First 'a' is required, then 'b' is required.
'a' | 'b'   Either 'a' is required, or 'b' is required.
'a'*    Zero or more 'a' are required.
'a'+    One or more 'a' are required.
<abba>  The rule called abba is required.

The polish notation is written like this:

     "                                                                                       \
number:    /-?[0-9]+/'.'?/[0-9]+/ ;                                                \
operator:  '+' | '-' | '*' | '/' | '%' | \"add\" | \"sub\" | \"mul\" | \"div\" ;           \
expr:      <number> | '(' <operator> <expr>+ ')' ;                                            \
dlispy:    /^/ <operator> <expr>+ /$/ ;",

I've managed to make it accept decimal numbers by adding '.'?/[0-9]+/, but i can't work out how to restructure it to accept standard notation eg 2*(3+2) instead of *2 (+ 3 2). I know that I'll have to rewrite the expr and the dlispy rules, but I'm new to regex and BNF. Hope you can help, thanks

fewlinesofcode
  • 3,007
  • 1
  • 13
  • 30
db24624
  • 1
  • 1

1 Answers1

1

Written as yacc rules, that would be:

expr    : '(' expr ')
        | expr operator expr
        | number
        ;
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41