本文整理汇总了Python中scanner.Scanner.build方法的典型用法代码示例。如果您正苦于以下问题:Python Scanner.build方法的具体用法?Python Scanner.build怎么用?Python Scanner.build使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scanner.Scanner
的用法示例。
在下文中一共展示了Scanner.build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", "IFX"),
("nonassoc", "ELSE"),
("right", "="),
("left", "OR"),
("left", "AND"),
("left", "|"),
("left", "^"),
("left", "&"),
("nonassoc", "<", ">", "EQ", "NEQ", "LE", "GE"),
("left", "SHL", "SHR"),
("left", "+", "-"),
("left", "*", "/", "%"),
)
def p_error(self, p):
if not p:
print("Syntax error at end of input")
else:
pass
def handle_error(self, type, p):
print(
"Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(
p.lineno, self.scanner.find_tok_column(p), p.type, p.value
)
)
def p_program(self, p):
"""program : declarations fundefs_opt instructions_opt"""
p[0] = Program(p[1], p[2], p[3])
# declarations
def p_declarations(self, p):
"""declarations : declarations declaration"""
if p[2]:
p[0] = DeclarationList(p[1].dec_list + [p[2]])
else:
p[0] = p[1]
def p_no_declarations(self, p):
"""declarations : """
p[0] = DeclarationList([])
# END declarations
# declaration
def p_declaration(self, p):
"""declaration : TYPE inits ';'"""
p[0] = Declaration(p[1], p[2])
def p_error_declaration(self, p):
"""declaration : error ';'"""
self.handle_error("declaration", p[1])
# END declaration
# inits
def p_inits(self, p):
"""inits : inits ',' init"""
p[0] = InitList(p[1].init_list + [p[3]])
def p_single_init(self, p):
"""inits : init """
p[0] = InitList([p[1]])
# END inits
def p_init(self, p):
"""init : ID '=' expression """
p[0] = Init(p[1], p[3])
# instructions_opt
def p_instructions_opt(self, p):
"""instructions_opt : instructions"""
p[0] = p[1]
def p_empty_instructions_opt(self, p):
"""instructions_opt : """
p[0] = InstructionList([])
# END instructions_opt
# instructions
def p_instructions(self, p):
"""instructions : instructions instruction"""
p[0] = InstructionList(p[1].instr_list + [p[2]])
#.........这里部分代码省略.........
示例2: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print('At end of input')
def p_program(self, p):
"""program : children children children"""
declarations = None if len(p[1].declarations) == 0 else p[1]
fundefs = None if len(p[2].fundefs) == 0 else p[2]
print AST.Program(declarations, fundefs, p[3])
def p_declarations(self, p):
"""children : children declaration
| """
if len(p) == 3:
p[0] = AST.DeclarationList() if p[1] is None else p[1]
p[0].addDeclaration(p[2])
else:
p[0] = AST.DeclarationList()
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if len(p) == 4:
type = p[1]
inits = p[2]
p[0] = AST.Declaration(type, inits)
def p_inits(self, p):
"""children : children ',' init
| init """
if len(p) == 4:
p[0] = AST.InitList() if p[1] is None else p[1]
p[0].addInit(p[3])
else:
p[0] = AST.InitList()
p[0].addInit(p[1])
def p_init(self, p):
"""init : ID '=' expression """
id = p[1]
expr = p[3]
p[0] = AST.Init(id, expr)
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
if len(p) == 3:
p[0] = AST.InstructionList() if p[1] is None else p[1]
p[0].addInstruction(p[2])
else:
p[0] = AST.InstructionList()
p[0].addInstruction(p[1])
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
| repeat_instr
| return_instr
| break_instr
| continue_instr
| compound_instr"""
p[0] = p[1]
def p_print_instr(self, p):
"""print_instr : PRINT expression ';'
| PRINT error ';' """
expr = p[2]
p[0] = AST.PrintInstruction(expr)
def p_labeled_instr(self, p):
"""labeled_instr : ID ':' instruction """
#.........这里部分代码省略.........
示例3: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.no_error = True
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
self.no_error = False
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno,
self.scanner.find_tok_column(p),
p.type,
p.value))
else:
print("Unexpected end of input")
def p_program(self, p):
"""program : blocks"""
p[0] = AST.Program(p[1])
p[0].line = self.scanner.lexer.lineno
if self.no_error:
# p[0].print_tree(0)
pass
def p_blocks(self, p):
"""blocks : blocks block
| block """
if len(p) > 2:
p[0] = AST.Blocks(p[2], p[1])
else:
p[0] = AST.Blocks(p[1], None)
p[0].line = self.scanner.lexer.lineno
def p_block(self, p):
"""block : fundef
| instruction
| declaration """
p[0] = p[1]
p[0].line = self.scanner.lexer.lineno
# def p_declarations(self, p):
# """declarations : declarations declaration
# | """
# if self.no_error:
# if len(p) > 1:
# p[0] = AST.Declarations(p[1], p[2])
# else:
# p[0] = AST.Declarations(None, None)
# p[0].line = self.scanner.lexer.lineno
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if len(p) > 3:
p[0] = AST.Declaration(p[1], p[2], None)
else:
p[0] = AST.Declaration(None, None, p[1])
p[0].line = self.scanner.lexer.lineno
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) > 2:
p[0] = AST.Inits(p[1], p[3])
else:
p[0] = AST.Inits(None, p[1])
p[0].line = self.scanner.lexer.lineno
def p_init(self, p):
"""init : ID '=' expression """
p[0] = AST.Init(p[1], p[3])
p[0].line = self.scanner.lexer.lineno
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
#.........这里部分代码省略.........
示例4: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.symbol_table = SymbolTable.SymbolTable("main_table")
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
operation_results = {
#~ ARITHMETIC OPERATORS ----------------------
"+": {
"int": {
"int": "int",
"float": "float",
"string": None,
"bool": None
},
"float": {
"int": "float",
"float": "float",
"string": None,
"bool": None
},
"string": {
"int": None,
"float": None,
"string": "string",
"bool": None
},
"bool": {
"int": None,
"float": None,
"string": None,
"bool": None
}
},
"-": {
"int": {
"int": "int",
"float": "float",
"string": None,
"bool": None
},
"float": {
"int": "float",
"float": "float",
"string": None,
"bool": None
},
"string": {
"int": None,
"float": None,
"string": None,
"bool": None
},
"bool": {
"int": None,
"float": None,
"string": None,
"bool": None
}
},
"*": {
"int": {
"int": "int",
"float": "int",
"string": None,
"bool": None
},
"float": {
"int": "float",
"float": "float",
"string": None,
"bool": None
},
"string": {
"int": "string",
"float": None,
"string": None,
"bool": None
},
"bool": {
#.........这里部分代码省略.........
示例5: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.errorsOccured = False
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno,
self.scanner.find_tok_column(p),
p.type, p.value))
else:
print('At end of input')
self.errorsOccured = True
def p_program(self, p):
"""program : classdefs declarations fundefs instructions"""
p[0] = AST.Program(p[1], p[2], p[3], p[4])
def p_declarations(self, p):
"""declarations : declarations declaration
| """
if len(p) > 1:
p[1].list.append(p[2])
p[0] = p[1]
else:
p[0] = AST.Declarations()
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| id classinits ';'
| error ';' """
if len(p) > 2:
p[0] = AST.Declaration(p[1], p[2])
else:
p[0] = p[1]
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) > 2:
p[1].list.append(p[3])
p[0] = p[1]
else:
inits = AST.Inits()
inits.list.append(p[1])
p[0] = inits
def p_init(self, p):
"""init : id '=' expression """
p[0] = AST.Init(p[1], p[3])
def p_classinits(self, p):
"""classinits : classinits ',' classinit
| classinit """
if len(p) > 2:
p[1].list.append(p[3])
p[0] = p[1]
else:
classinits = AST.Classinits()
classinits.list.append(p[1])
p[0] = classinits
def p_classinit(self, p):
"""classinit : id """
p[0] = AST.Classinit(p[1])
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
if len(p) == 2:
instructions = AST.Instructions()
instructions.list.append(p[1])
p[0] = instructions
else:
p[1].list.append(p[2])
p[0] = p[1]
#.........这里部分代码省略.........
示例6: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.operation_results = OperationResults.operation_results
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print("Unexpected end of input")
def p_program(self, p):
"""program : blocks"""
program = AST.Program(p[1])
p[0] = program
def p_blocks(self, p):
"""blocks : blocks block
| block"""
if(len(p) > 2):
if(isinstance(p[2], list)):
p[1].extend(p[2])
else:
p[1].append(p[2])
p[0] = p[1]
else:
if(isinstance(p[1], list)):
p[0] = p[1]
else:
p[0] = [p[1]]
def p_block(self, p):
"""block : declaration
| fundef
| instruction"""
p[0] = p[1]
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if(len(p) > 2):
p[0] = p[2]
declared_type = p[1]
for i in range(len(p[0])):
p[0][i].left.type = declared_type
else:
p[0] = p[1]
def p_inits(self, p):
"""inits : inits ',' init
| init """
if(len(p) > 3):
p[1].append(p[3])
p[0] = p[1]
else:
p[0] = [p[1]]
def p_init(self, p):
"""init : ID '=' expression """
decl = AST.Declaration(AST.Id("UNKNOWN", p[1], p.lineno(1)), p[3], p.lineno(1))
p[0] = decl
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
#.........这里部分代码省略.........
示例7: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.tree = None
tokens = Scanner.tokens
# precedence = (
# ("nonassoc", 'IFX'),
# ("nonassoc", 'ELSE'),
# ("right", '='),
# ("left", 'OR'),
# ("left", 'AND'),
# ("left", '|'),
# ("left", '^'),
# ("left", '&'),
# ("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
# ("left", 'SHL', 'SHR'),
# ("left", '+', '-'),
# ("left", '*', '/', '%'),
# )
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print('At end of input')
def p_program(self, p):
"""program : typeDeclarations"""
p[0] = AST.Program(p.lineno(1), p[1])
print p[0]
def p_typeDeclarations(self, p):
"""typeDeclarations : typeDeclaration
| typeDeclarations typeDeclaration"""
if len(p) == 2:
p[0] = AST.TypeDeclarations(p.lineno(1), p[1])
elif len(p) > 2:
p[0] = AST.TypeDeclarations(p.lineno(1), p[2], p[1])
def p_typeDeclaration(self, p):
"""typeDeclaration : classDeclaration
| interfaceDeclaration"""
p[0] = AST.TypeDeclaration(p.lineno(1), p[1])
def p_classDeclaration(self, p):
"""classDeclaration : classModifier CLASS ID inherited classBody"""
p[0] = AST.ClassDeclaration(p.lineno(1), p[1], p[3], p[4], p[5])
def p_classModifier(self, p):
"""classModifier : ACCESS modifier
| modifier
| ACCESS
| """
if len(p) == 2:
p[0] = AST.ClassModifier(p.lineno(1), p[1])
elif len(p) > 2:
p[0] = AST.ClassModifier(p.lineno(1), p[1], p[2])
else:
p[0] = AST.ClassModifier()
def p_modifier(self, p):
"""modifier : ABSTRACT
| FINAL"""
p[0] = AST.Modifier()
def p_inherited(self, p):
"""inherited : extends implements
| extends
| implements
| """
if len(p) == 2:
p[0] = AST.Inherited(p.lineno(1), p[1])
elif len(p) > 2:
p[0] = AST.Inherited(p.lineno(1), p[1], p[2])
else:
p[0] = AST.Inherited()
print p[0]
def p_extends(self, p):
"""extends : EXTENDS ID"""
p[0] = AST.Extend(p.lineno(1), p[2])
def p_implements(self, p):
"""implements : IMPLEMENTS implement """
p[0] = AST.Implements(p.lineno(1), p[2])
def p_implement(self, p):
"""implement : implement ',' ID
| ID """
if len(p) == 2:
p[0] = AST.Implement(p.lineno(1), p[1])
#.........这里部分代码省略.........
示例8: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
palka = ''
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
error = 0
def p_error(self, p):
Cparser.error = 1
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
#exit(1);
else:
print('At end of input')
def p_program(self, p):
"""program : declarations fundefs instructions"""
p[0] = AST.Program(p[1], p[2], p[3])
#print(p[0].toString(0))
p[1].line = self.scanner.lexer.lineno
p[1].line = self.scanner.lexer.lineno
def p_declarations(self, p):
"""declarations : declarations declaration"""
p[0] = AST.Declarations(p[1], p[2])
p[0].line = self.scanner.lexer.lineno
def p_declarations_1(self, p):
"""declarations : """
p[0] = AST.Empty()
p[0].line = self.scanner.lexer.lineno
def p_declaration(self, p):
"""declaration : TYPE inits ';' """
p[0] = AST.Declaration(p[1], p[2])
p[0].line = self.scanner.lexer.lineno
def p_declaration_error(self, p):
"""declaration : error ';' """
self.p_error(p[1])
def p_inits(self, p):
"""inits : inits ',' init"""
p[0] = AST.Inits(p[1], p[3])
p[0].line = self.scanner.lexer.lineno
def p_inits_1(self, p):
"""inits : init"""
p[0] = AST.Inits(None, p[1])
p[0].line = self.scanner.lexer.lineno
def p_init(self, p):
"""init : ID '=' expression """
p[0] = AST.Init(p[1], p[3])
p[0].line = self.scanner.lexer.lineno
def p_instructions(self, p):
"""instructions : instructions instruction"""
p[0] = AST.Instructions(p[1], p[2])
p[0].line = self.scanner.lexer.lineno
def p_instructions_1(self, p):
"""instructions : instruction"""
p[0] = AST.Instructions(None, p[1])
p[0].line = self.scanner.lexer.lineno
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
| repeat_instr
| return_instr
| break_instr
| continue_instr
| compound_instr"""
p[0] = AST.Instruction(p[1])
#.........这里部分代码省略.........
示例9: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno(1), self.scanner.find_tok_column(p), p.type, p.value))
else:
print("Unexpected end of input")
#do nowej gramatyki:
def p_program(self, p):
"""program : blocks"""
p[0] = p[1]
# print p[0]
#do nowej gramatyki:
def p_blocks(self, p):
"""blocks : blocks block
| block"""
if len(p) != 3:
#after |
p[0] = AST.Blocks()
p[0].push(p[1])
else:
if p[1] is None:
p[0] = AST.Blocks()
else:
p[0] = p[1]
p[0].push(p[2])
#do nowej gramatyki:
def p_block(self, p):
"""block : declarations fundefs_opt instructions_opt"""
p[0] = AST.Block(p[1], p[2], p[3])
def p_declarations(self, p):
"""declarations : declarations declaration
| """
if len(p) != 3:
#after |
p[0] = AST.Declarations()
else:
#put right `declarations` to left `declarations` and append `declaration`
if p[1] is None:
p[0] = AST.Declarations()
else:
p[0] = p[1]
p[0].push(p[2])
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if len(p) == 4:
type = p[1]
inits = p[2]
p[0] = AST.Declaration(type, inits, p.lineno(1))
#variable initialization
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) == 4:
p[0] = p[1]
p[0].push(p[3])
else:
p[0] = AST.Inits()
p[0].push(p[1])
def p_init(self, p):
"""init : ID '=' expression """
p[0] = AST.Init(p[1], p[3], p.lineno(1))
#.........这里部分代码省略.........
示例10: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.errors = False
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
self.errors = True
err_format = "Syntax error at line {0}, column {1}: LexToken({2}, '{3}')"
if p:
print(err_format.format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print('At end of input')
def p_program(self, p):
"""program : declarations fundefs instructions"""
# ^ ^ ^ ^
# p[0] p[1] p[2] p[3]
program = AST.Program(p[1], p[2], p[3])
p[0] = program
def p_declarations(self, p):
"""declarations : declarations declaration
| """
if len(p) == 3: # occurs when declarations -> declarations declaration
p[1].declarations.append(p[2])
p[0] = p[1]
else: # occurs when declarations -> epsilon
p[0] = AST.Declarations()
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if len(p) == 3: # occurs when error
p[0] = p[1]
else:
p[0] = AST.Declaration(p[1], p[2])
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) == 4: # occurs when inits -> inits, init
p[0] = p[1]
p[0].inits.append(p[3])
else: # occurs when inits -> init
p[0] = AST.Inits()
p[0].inits.append(p[1])
def p_init(self, p):
"""init : ID '=' expression """
p[0] = AST.Init(p[1], p[3], p.lineno(1))
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
if len(p) == 3: # occurs when instructions -> instructions instruction
p[1].instructions.append(p[2])
p[0] = p[1]
else: # occurs when instructions -> instruction
p[0] = AST.Instructions()
p[0].instructions.append(p[1])
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
| repeat_instr
| return_instr
| break_instr
| continue_instr
| compound_instr"""
p[0] = p[1]
def p_print_instr(self, p):
"""print_instr : PRINT expression ';'
| PRINT error ';' """
p[0] = AST.PrintInstr(p[2], p.lineno(1))
def p_labeled_instr(self, p):
"""labeled_instr : ID ':' instruction """
p[0] = AST.LabeledInstruction(p[1], p[3], p.lineno(1))
#.........这里部分代码省略.........
示例11: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IF'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '^'),
("nonassoc", '<', '>', '=', 'NEQ', 'LE', 'GE'),
("left", '+', '-'),
("left", '*', '/', 'MOD', 'DIV'),
)
error_encountered = False
def convert_from_string(self, value, lineno):
try:
return Integer(int(value), lineno)
except ValueError:
try:
return Float(float(value), lineno)
except ValueError:
return String(value, lineno)
def p_error(self, p):
self.error_encountered = True
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno,
self.scanner.find_tok_column(p),
p.type, p.value))
else:
print('At end of input')
def p_program(self, p):
"""program : program_header declarations compound_statement '.'"""
p[0] = Program(p[1], p[2], p[3])
def p_program_header(self, p):
"""program_header : PROGRAM_LITERAL ID ';'
| PROGRAM_LITERAL ID '(' id_list ')' ';' """
if len(p) == 4:
p[0] = ProgramHeader(p[2])
else:
p[0] = ProgramHeader(p[2], p[4])
def p_declarations(self, p):
"""declarations : constant_definitions type_definitions variable_declarations procedure_declarations"""
p[0] = Declarations(p[1], p[2], p[3], p[4])
def p_constant_definitions(self, p):
"""constant_definitions : CONST constant_definition_list
| """
if len(p) == 1:
p[0] = list()
else:
p[0] = p[2]
def p_constant_definition_list(self, p):
"""constant_definition_list : constant_definition_list const_def
| const_def """
if len(p) == 2:
p[0] = list()
p[0].append(p[1])
else:
p[1].append(p[2])
p[0] = p[1]
def p_const_def(self, p):
"""const_def : ID '=' CONSTANT ';'"""
const_value = self.convert_from_string(p[3], p.lineno(1))
p[0] = ConstDef(const_value, p[1], p.lineno(1))
def p_type_definitions(self, p):
"""type_definitions : TYPE type_definition_list
| """
if len(p) == 1:
p[0] = list()
else:
p[0] = p[2]
def p_type_definition_list(self, p):
"""type_definition_list : type_definition_list type_def
| type_def"""
if len(p) == 2:
p[0] = list()
p[0].append(p[1])
else:
#.........这里部分代码省略.........
示例12: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
self.error = False
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
self.error = True
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
while 1:
tok = yacc.token()
if not tok or tok.type == 'RBRACE': break
yacc.restart()
else:
print('At end of input')
def p_program(self, p):
"""program : declarations fundefs instructions"""
p[0] = AST.Program(p[1], p[2], p[3], p.lineno(1))
def p_declarations(self, p):
"""declarations : declarations declaration
| """
if len(p) > 2:
p[0] = p[1]
p[1].append(p[2])
else:
p[0] = []
def p_declaration(self, p):
"""declaration : TYPE inits ';'"""
if len(p) > 3:
p[0] = AST.Variable(p[1], p[2], p.lineno(1))
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) > 2:
p[0] = p[1]
p[1].append(p[3])
else:
p[0] = [p[1]]
def p_init(self, p):
"""init : ID '=' expression
| ID """
if len(p) > 3:
p[0] = AST.Declaration(p[1], p[2], p[3], p.lineno(1))
else:
p[0] = AST.Declaration(p[1], None, None, p.lineno(1))
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction
| """
if len(p) > 2:
p[0] = p[1]
p[1].append(p[2])
elif len(p) == 2:
p[0] = [p[1]]
else:
p[0] = []
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
| repeat_instr
| return_instr
| break_instr
#.........这里部分代码省略.........
示例13: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print('At end of input\nMost likely you haven\'t included a single instruction')
def p_program(self, p):
"""program : declarations fundefs instructions"""
p[0] = AST.AST(p[1], p[2], p[3])
def p_declarations(self, p):
"""declarations : declarations declaration
| """
try:
p[0] = p[1] + [p[2]]
except IndexError:
p[0] = []
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
try:
p[3]
p[0] = AST.Declaration(p[1], p[2])
except IndexError:
p[0] = AST.Error(p[1])
def p_inits(self, p):
"""inits : inits ',' init
| init """
try:
p[0] = p[1] + [p[3]]
except IndexError:
p[0] = [p[1]]
def p_init(self, p):
"""init : ID '=' expression """
p[0] = self.add_pos(AST.Init(self.add_pos(AST.Variable(p[1]), p), p[3]), p)
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
try:
p[0] = p[1] + [p[2]]
except IndexError:
p[0] = [p[1]]
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
| repeat_instr
| return_instr
| break_instr
| continue_instr
| compound_instr"""
p[0] = p[1]
def p_print_instr(self, p):
"""print_instr : PRINT expression ';'
| PRINT error ';' """
p[0] = AST.Print(p[2])
#.........这里部分代码省略.........
示例14: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno,
self.scanner.find_tok_column(p),
p.type, p.value))
else:
print("Unexpected end of input")
def p_program(self, p):
"""program : instruction_list"""
p[0] = AST.Program(p[1])
def p_instruction_list(self, p):
"""instruction_list : instruction_list instruction_item
| """
if len(p) == 3:
p[0] = list(p[1]) if p[1] else []
p[0].append(p[2])
else:
p[0] = []
def p_instruction_item(self, p):
"""instruction_item : declaration
| fundef
| instruction"""
p[0] = p[1]
def p_declarations(self, p):
"""declarations : declarations declaration
| """
if len(p) == 3:
p[0] = list(p[1]) if p[1] else []
p[0].append(p[2])
else:
p[0] = []
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if len(p) == 4:
p[0] = AST.Declaration(AST.Name(p[1]), p[2])
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) == 4:
p[0] = list(p[1])
p[0].append(p[3])
else:
p[0] = [p[1]]
def p_init(self, p):
"""init : ID '=' expression """
p[0] = AST.Initializer(AST.Name(p[1]), p[3])
def p_instructions_opt(self, p):
"""instructions_opt : instructions
| """
if len(p) == 2:
p[0] = list(p[1])
else:
p[0] = []
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
if len(p) == 3:
p[0] = list(p[1])
p[0].append(p[2])
else:
p[0] = [p[1]]
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
#.........这里部分代码省略.........
示例15: Cparser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import build [as 别名]
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print('At end of input')
def p_program(self, p):
"""program : declarations fundefs instructions"""
def p_declarations(self, p):
"""declarations : declarations declaration
| """
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
def p_inits(self, p):
"""inits : inits ',' init
| init """
def p_init(self, p):
"""init : ID '=' expression """
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
def p_instruction(self, p):
"""instruction : print_instr
| labeled_instr
| assignment
| choice_instr
| while_instr
| repeat_instr
| return_instr
| break_instr
| continue_instr
| compound_instr"""
def p_print_instr(self, p):
"""print_instr : PRINT expression ';'
| PRINT error ';' """
def p_labeled_instr(self, p):
"""labeled_instr : ID ':' instruction """
def p_assignment(self, p):
"""assignment : ID '=' expression ';' """
def p_choice_instr(self, p):
"""choice_instr : IF '(' condition ')' instruction %prec IFX
| IF '(' condition ')' instruction ELSE instruction
| IF '(' error ')' instruction %prec IFX
| IF '(' error ')' instruction ELSE instruction """
def p_while_instr(self, p):
"""while_instr : WHILE '(' condition ')' instruction
| WHILE '(' error ')' instruction """
#.........这里部分代码省略.........