コード例 #1
0
def generate():
    rawCFG = request.form['CFG']
    typedef = TypeDefinition.load("simpleJava/typedef")
    cfg = ContextFreeGrammar.loadFromString(typedef, rawCFG)
    action, goto, rawItemToID = Parser.genActionGoto(typedef, cfg, needItemToID=True)
    terminals, nonTerminals = action.terminals(), goto.nonTerminals()
    symbols = terminals + nonTerminals
    result = [["state"] + symbols]
    app.typedef = typedef
    app.cfg = cfg
    app.action = action  # should be session or cookie... but I don't know how
    app.goto = goto

    for i in range(len(action)):
        result.append([str(i)] + [str(action[i][k]) for k in terminals] + [str(goto[i][k]) for k in nonTerminals])

    itemToID = {}
    for k, v in rawItemToID.items():
        itemToID[Parser.toStr(typedef, k)] = v
    
    cfgForFirst = cfg.removeLeftRecursion() if cfg.isLeftRecursive() else cfg
    firstDict = Parser.first(cfgForFirst)
    firstSet = {k: ', '.join([typedef.getDisplayName(sym) for sym in v])
                for k, v in firstDict.items()}

    return render_template(
        "parse_result.html", 
        itemToID=itemToID,
        table=result,
        firstSet=firstSet
    )
コード例 #2
0
ファイル: Parser.py プロジェクト: medioqrity/newCompilerTest
            elif actionType == 2:
                if needLog:
                    log.append((str(stateStack), str(nodeStack), "ACCEPTED"))
                break
            else:
                assert False

        # print(stateStack, nodeStack, tokenType, actionType, nextState)
    # print(nodeStack)
    if needLog:
        return ParseTree(nodeStack[-1]), log
    return ParseTree(nodeStack[-1])


if __name__ == "__main__":
    typedef = TypeDefinition.load(FOLDER + "typedef")
    cfg = ContextFreeGrammar.load(typedef, FOLDER + "simpleJavaCFG")
    # action.save(FOLDER + "simpleJavaAction")
    # goto.save(FOLDER + "simpleJavaGoto")
    # exit()
    action = Action.load(cfg, FOLDER + "simpleJavaAction")
    goto = Goto.load(cfg, FOLDER + "simpleJavaGoto")

    with open(FOLDER + "test.sjava", "r") as f:
        src = f.read()
    tokenList = scanner.parse(typedef, src,
                              ['line_comment', 'block_comment', 'space'])
    print(tokenList)
    pt = parse(tokenList, typedef, cfg, action, goto)
    print(pt)
コード例 #3
0
from cfg import ContextFreeGrammar
from typeDef import TypeDefinition
import Parser

typedef = TypeDefinition.load("simpleCalc/typedef")
cfg = ContextFreeGrammar.load(typedef, "simpleCalc/CFG4")
action, goto = Parser.genActionGoto(typedef, cfg)
action.save('simpleCalc/calc_action')
goto.save('simpleCalc/calc_goto')
exit()
コード例 #4
0
import re
from typeDef import TypeDefinition


def parse(typeDef, src_code, filter_=None):
    """
    return a list of Token object, generated by the input src_code
    if want to filter out any type of token, use filter.
    eg:
        parse(typedef, src, ['block_comment', 'line_comment'])
    """
    assert isinstance(typeDef, TypeDefinition)
    result = []
    token_regex = typeDef.getRE()
    for mo in re.finditer(token_regex, src_code):
        if filter_ is None or mo.lastgroup not in filter_:
            id_ = typeDef.getID(mo.lastgroup)
            result.append((str(mo[0]), id_))
    result.append(("$", "$"))
    return result


if __name__ == "__main__":
    typedef = TypeDefinition.load("simpleJava/typedef")
    with open("simpleJava/test.sjava", "r") as f:
        src_code = f.read()
    print(parse(typedef, src_code, ['space']))
コード例 #5
0
ファイル: sql.py プロジェクト: medioqrity/newCompilerTest
from cfg import ContextFreeGrammar
from typeDef import TypeDefinition
from action import Action
from goto import Goto
from parseTree import ParseTreeActionRegister
import scanner
import Parser

typedef = TypeDefinition.load("simpleSQL/sqltypedef")
cfg = ContextFreeGrammar.load(typedef, "simpleSQL/SQL")
action, goto = Parser.genActionGoto(typedef, cfg)

tokenList = scanner.parse(typedef, input())
pt = Parser.parse(tokenList, typedef, cfg, action, goto)
print(pt)