没有获得使用 python ply 库检查 R 编程语言的 while 循环语法的预期输出

Not getting expected output for checking syntax of while loops of R programming language using python ply library

提问人:Vis 提问时间:11/7/2023 更新时间:11/7/2023 访问量:32

问:

我编写了一个 python 代码来检查 R 编程语言中 while 循环的语法。

代码如下:

import ply.lex as lex
import ply.yacc as yacc

tokens = ('NUMBER',
          'STRING',
          'IDENTIFIER',
          'LBRACKET',
          'RBRACKET',
          'LFLOWER',
          'RFLOWER',
          'PLUS',
          'MINUS',
          'STAR',
          'SLASH',
          'CAP',
          'EQUAL',
          'NOT',
          'GREATER',
          'LESSER',
          'SEMI_COLON',
          'POINT',
          'AND',
          'OR',
          'TRUE',
          'FALSE',
          'IF',
          'ELSE',
          'WH')

t_ignore = ' \t\n'

def t_IDENTIFIER(t):
    r'[a-zA-Z_][a-zA-Z0-9_]*'
    return t

def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)
    return t

def t_STRING(t):
    r'"[^"]*"'
    return t

t_LBRACKET = r'\('
t_RBRACKET = r'\)'
t_SEMI_COLON = r';'
t_EQUAL = r'='
t_LFLOWER = r'\{'
t_RFLOWER = r'\}'
t_LESSER = r'<'
t_GREATER = r'>'
t_NOT = r'!'
t_AND = r'&&'
t_OR = r'\|\|'
t_PLUS = r'\+'
t_MINUS = r'-'
t_STAR = r'\*'
t_SLASH = r'/'
t_CAP = r'\^'
t_POINT = r'<-'
t_TRUE = 'TRUE'
t_FALSE = 'FALSE'
t_IF = 'if'
t_ELSE = 'else'
t_WH = 'while'

def t_error(t):
    print(f"Illegal character found '{t.value[0]}'")
    t.lexer.skip(1)

lexer = lex.lex()

def mylex(inp):
    lexer.input(inp)

    for token in lexer:
        print("Token:", token)

flag = 0

# while ( i = = 5 ) { num <- 5 ; num1 <- 6 }

def p_while(p):
    '''
    WHILE   : WH LBRACKET CONDITION RBRACKET STATEMENT_BLOCK
    '''

def p_condition(p):
    '''
    CONDITION   : EXPRESSION
    '''

def p_statement_block(p):
    '''
    STATEMENT_BLOCK : LFLOWER STATEMENT_LIST RFLOWER
    '''

def p_expression(p):
    '''
    EXPRESSION  : LITERAL
                | VARIABLE
                | BINARY_OPERATION
                | UNARY_OPERATION
    '''

def p_literal(p):
    '''
    LITERAL : NUMBER
            | STRING
            | BOOLEAN
    '''

def p_variable(p):
    '''
    VARIABLE    : IDENTIFIER
    '''

def p_binary_operation(p):
    '''
    BINARY_OPERATION    : EXPRESSION BINARY_OPERATOR EXPRESSION
    '''

def p_unary_operation(p):
    '''
    UNARY_OPERATION : UNARY_OPERATOR EXPRESSION
    '''

def p_boolean(p):
    '''
    BOOLEAN : TRUE
            | FALSE
    '''

def p_binary_operator(p):
    '''
    BINARY_OPERATOR : PLUS
                    | MINUS
                    | STAR
                    | SLASH
                    | CAP
                    | EQUAL EQUAL
                    | NOT EQUAL
                    | GREATER
                    | LESSER
                    | GREATER EQUAL
                    | LESSER EQUAL
                    | AND
                    | OR
    '''

def p_unary_operator(p):
    '''
    UNARY_OPERATOR  : NOT
                    | MINUS
    '''

def p_statement_list(p):
    '''
    STATEMENT_LIST  : STATEMENT SEMI_COLON STATEMENT_LIST
                    | STATEMENT
    '''

def p_statement(p):
    '''
    STATEMENT   : ASSIGNMENT
                | WHILE
                | IF_STATEMENT
                | EXPRESSION
    '''

def p_assignment(p):
    '''
    ASSIGNMENT  : IDENTIFIER POINT EXPRESSION
    '''

def p_if_statement(p):
    '''
    IF_STATEMENT    : IF LBRACKET EXPRESSION RBRACKET STATEMENT_BLOCK ELSE_CLAUSE
    '''

def p_else_clause(p):
    '''
    ELSE_CLAUSE : ELSE STATEMENT_BLOCK
                | EMPTY
    '''

def p_empty(p):
    '''
    EMPTY   : 
    '''

def p_error(p):
    print("Syntax error")
    global flag
    flag = 1

parser = yacc.yacc()
while True:
   flag = 0
   try:
       s = input('enter the conditional statement:')
   except EOFError:
       break
   if not s: 
            flag = 0
            continue
   result = parser.parse(s)
   if flag == 0:
        print("Valid syntax")
        print("Result:", result)

在提供以下输入时:

enter the conditional statement:while ( i == 5 ) { num <- 5 ; num1 <- 6 }

即使遵循了语法,我也收到“语法错误”作为输出。任何人都可以帮助找到导致此错误输出的代码中的错误吗?

Python R 解析

评论


答: 暂无答案