def __init__(self, grammar, lexer): self.grammar = grammar self.glexer = glexer self.mp = MetaParser(grammar, glexer, False) self.sm = LLStateMachine(self.mp) self.table = table self.start = self.mp.productions.start
def __init__(self, grammar): self.grammar = grammar # GRAMMAR FOR LANGUAGE grammar_lexer = MyLexer(grammar) grammar_lexer.lex() self.mp = MetaParser(grammar, grammar_lexer, False) self.sm = LLStateMachine(self.mp) self.table = self.sm.table self.start = self.mp.productions.start
def test_grammar(grammar): global errors Symbol.reset() print('=' * 80) print('{0:=^80}'.format(' Testing Grammar ')) print('=' * 80) print(grammar) try: print("Creating Lexer") lexer = Lexer(grammar) print("Creating MetaParser") mp = MetaParser(grammar, lexer) except Exception as e: print("ERROR:") print(e) errors += 1
from cup.lexer.lexer import Lexer from cup.parser.metaparser import MetaParser from cup.lexer.tokens import * from cup.parser.symbols import Terminal, NonTerminal, Production from cup.parser.statemachine import Item, State, LRStateMachine from cup.utils.helper import print_banner with open("test/grammar.bcup") as f: grammar = f.read() lexer = Lexer(grammar) mp = MetaParser(grammar, lexer) print_banner("TERMINALS") print(mp.terminals) print_banner("NONTERMINALS") print(mp.nonterminals) print(mp.token_map) print_banner("PRODUCTIONS") print(mp.productions) mp.compute_firsts() mp.compute_follows() follows = mp.productions.follows firsts = mp.productions.firsts print_banner("FIRSTS") for key in firsts: s = "FIRST({}) = {{".format(key)