How can I define ε when I'm writing the grammar rules in parsing code?

68 views Asked by At

I'm trying to write a compiler program for a specific grammar I defined.

There are a few ε in the grammar because of some iterative and recursive rules. I tried to define ε by creating an empty token:

tokens = (
    'EMPTY'
) 

t_EMPTY = r'\ ' 

That caused some parsing problems, therefore I ignored spaces earlier in the lexer code:

t_ignore  = ' \t' 

What are other ways to describe define ε?

Click here to see the project files

1

There are 1 answers

0
AudioBubble On

Solved.The yacc.py module I used includes a special <empty> value for ε. Here is empty defining in the grammar part of the code:

def p_empty(p):
    'empty :'
    pass

def p_option(p):
    '''option : start 
              | empty '''