+ 1

Develop a visual application for lexical and syntactical analysis of a predicate term

Develop a visual application for lexical and syntactical analysis of a predicate term. If correct, it should display all lexical units. Syntax of the term: Constant: Natural number or string enclosed in single quotes (' ') Variable: Identifier Function: Identifier (term1, ..., termn) / n > 1, Allows spaces.

16th Oct 2023, 7:40 PM
Aziza ibtissam Boulanouar
Aziza ibtissam Boulanouar - avatar
1 Respuesta
0
import ply.lex as lex import ply.yacc as yacc # Define the tokens tokens = ( 'CONSTANT', 'VARIABLE', 'FUNCTION', ) # Regular expressions for tokens t_CONSTANT = r"'[^']*'" t_VARIABLE = r'[a-zA-Z_][a-zA-Z0-9_]*' t_FUNCTION = r'[a-zA-Z_][a-zA-Z0-9_]*\s*\([^)]+\)' # Define a parser def p_predicate_term(p): '''predicate_term : CONSTANT | VARIABLE | FUNCTION''' p[0] = p[1] # Error handling def p_error(p): print("Syntax error in input!") # Build the lexer and parser lexer = lex.lex() parser = yacc.yacc() # Input from the user input_term = input("Enter a predicate term: ") # Lexical analysis lexer.input(input_term) for token in lexer: print(f"Token: {token.type}, Value: {token.value}") # Syntactical analysis parsed_term = parser.parse(input_term) if parsed_term: print("Syntax is correct.") else: print("Syntax is incorrect.")
23rd Oct 2023, 9:46 AM
Coderwe2
Coderwe2 - avatar