uppaal2jetracer.parser.declarationparser package

Subpackages

Submodules

uppaal2jetracer.parser.declarationparser.declaration_lexer module

This module provides a lexer for the UPPAAL declaration language. It leverages the PLY (Python Lex-Yacc) library to tokenize input strings according to the specific syntax rules of UPPAAL declarations. The lexer is implemented in the DeclarationLexer class, which includes support for handling custom errors, tracking file-related metadata, and identifying specific tokens such as keywords, identifiers, constants, operators, and string literals.

Based on the c_lexer.py from the pycparser project by Eli Bendersky (https://eli.thegreenplace.net) under the BSD license.

Classes:
  • DeclarationLexer: Implements a custom lexer for UPPAAL declarations with methods for input handling, token generation, and error handling.

Key Features:
  • Recognizes reserved keywords, custom identifiers, constants, and various operators.

  • Provides hooks for handling specific events like opening and closing braces.

  • Offers mechanisms to handle typographical lookups for typedef names.

Usage:

This lexer can be used to tokenize UPPAAL declaration files by creating an instance of DeclarationLexer, providing necessary callback functions, and calling its build, input, and token methods as needed.

class DeclarationLexer(error_func, on_lbrace_func, on_rbrace_func, type_lookup_func)[source]

Bases: object

A lexer for the UPPAAL declaration language. After building it, set the input text with input(), and call token() to get new tokens.

The public attribute filename can be set to an initial filename, but the lexer will update it upon #line directives.

Variables:
  • error_func – An error function. Will be called with an error message, line, and column as arguments, in case of an error during lexing.

  • on_lbrace_func – Function triggered when an opening brace ‘{’ is encountered.

  • on_rbrace_func – Function triggered when a closing brace ‘}’ is encountered.

  • type_lookup_func – A type lookup function. Given a string, it must return True if this string is a name of a type that was defined with a typedef earlier.

  • filename – Tracks the filename, updating it upon #line directives.

  • last_token – Keeps track of the last token returned from self.token().

  • line_pattern – A compiled regular expression for matching “# line” or “# <num>” patterns.

  • pragma_pattern – A compiled regular expression for matching “# pragma” patterns.

  • lexer – The PLY lexer object created after calling the build() method.

BAD_CHAR_CONST = '(\'([^\'\\\\\\n]|(\\\\(([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))|(\\d+)(?!\\d))))[^\'\\n]+\')|(\'\')|(\'([\\\\][^a-zA-Z._~^!=&\\^\\-\\\\?\'"x0-9])[^\'\\n]*\')'
BAD_ESCAPE = '([\\\\][^a-zA-Z._~^!=&\\^\\-\\\\?\'"x0-9])'
BOOL_CONST = '(true|false)'
CCONST_CHAR = '([^\'\\\\\\n]|(\\\\(([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))|(\\d+)(?!\\d))))'
DECIMAL_ESCAPE = '(\\d+)(?!\\d)'
ESCAPE_SEQUENCE_START_IN_STRING = '(\\\\[0-9a-zA-Z._~!=&\\^\\-\\\\?\'"])'
EXPONENT_PART = '([eE][-+]?[0-9]+)'
FRACTIONAL_CONSTANT = '([0-9]*\\.[0-9]+)|([0-9]+\\.)'
IDENTIFIER = '[a-zA-Z_][0-9a-zA-Z_]*'
SIMPLE_ESCAPE = '([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))'
STRING_CHAR = '([^"\\\\\\n]|(\\\\[0-9a-zA-Z._~!=&\\^\\-\\\\?\'"]))'
bad_string_literal = '"([^"\\\\\\n]|(\\\\[0-9a-zA-Z._~!=&\\^\\-\\\\?\'"]))*([\\\\][^a-zA-Z._~^!=&\\^\\-\\\\?\'"x0-9])([^"\\\\\\n]|(\\\\[0-9a-zA-Z._~!=&\\^\\-\\\\?\'"]))*"'
build(**kwargs)[source]

Builds the lexer from the specification. Must be called after the lexer object is created.

This method exists separately, because the PLY manual warns against calling lex.lex inside __init__

char_const = '\'([^\'\\\\\\n]|(\\\\(([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))|(\\d+)(?!\\d))))\''
decimal_constant = '(0)|([1-9][0-9]*)'
double_constant = '(((( ([0-9]*\\.[0-9]+)|([0-9]+\\.) )([eE][-+]?[0-9]+)?)|([0-9]+([eE][-+]?[0-9]+)))[FfLl]?)'
escape_sequence = '(\\\\(([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))|(\\d+)(?!\\d)))'
find_tok_column(token)[source]

Find the column of the token in its line.

input(text)[source]
keyword = 'WHILE'
keyword_map = {'bool': 'BOOL', 'broadcast': 'BROADCAST', 'chan': 'CHAN', 'char': 'CHAR', 'clock': 'CLOCK', 'const': 'CONST', 'do': 'DO', 'double': 'DOUBLE', 'else': 'ELSE', 'for': 'FOR', 'if': 'IF', 'int': 'INT', 'return': 'RETURN', 'string': 'STRING', 'struct': 'STRUCT', 'typedef': 'TYPEDEF', 'void': 'VOID', 'while': 'WHILE'}
keywords = ('BOOL', 'BROADCAST', 'CLOCK', 'CHAN', 'CHAR', 'STRING', 'CONST', 'DO', 'DOUBLE', 'ELSE', 'FOR', 'IF', 'INT', 'RETURN', 'STRUCT', 'TYPEDEF', 'VOID', 'WHILE')
reset_lineno()[source]

Resets the internal line number counter of the lexer.

string_literal = '"([^"\\\\\\n]|(\\\\[0-9a-zA-Z._~!=&\\^\\-\\\\?\'"]))*"'
t_AND = '&'
t_ANDEQUAL = '&='
t_ARROW = '->'
t_BAD_CHAR_CONST(t)[source]

Detects invalid character constants and raises an error.

t_BAD_STRING_LITERAL(t)[source]

Matches invalid string literals that contain invalid escape codes and raises an error.

t_BOOL_CONST(t)[source]

Matches and returns a boolean constant token.

t_CHAR_CONST(t)[source]

Matches and returns a regular character constant token.

t_COLON = ':'
t_COMMA = ','
t_DIVEQUAL = '/='
t_DIVIDE = '/'
t_DOUBLE_CONST(t)[source]

Matches and returns a floating-point constant token.

t_EQ = '=='
t_EQUALS = '='
t_GE = '>='
t_GT = '>'
t_ID(t)[source]

Matches identifiers and checks if they are keywords or type identifiers. Updates the token type accordingly.

t_INT_CONST_DEC(t)[source]

Matches and returns a decimal integer constant token.

t_LAND = '&&'
t_LBRACE(t)[source]

Handles the opening brace “{” token. Triggers the on_lbrace_func to update the scope.

t_LBRACKET = '\\['
t_LE = '<='
t_LNOT = '!'
t_LOR = '\\|\\|'
t_LPAREN = '\\('
t_LSHIFT = '<<'
t_LSHIFTEQUAL = '<<='
t_LT = '<'
t_MINUS = '-'
t_MINUSEQUAL = '-='
t_MOD = '%'
t_MODEQUAL = '%='
t_NE = '!='
t_NEWLINE(t)[source]

n+

t_NOT = '~'
t_OR = '\\|'
t_OREQUAL = '\\|='
t_PERIOD = '\\.'
t_PLUS = '\\+'
t_PLUSEQUAL = '\\+='
t_RBRACE(t)[source]

Handles the closing brace “}” token. Triggers the on_rbrace_func to update the scope.

t_RBRACKET = '\\]'
t_RPAREN = '\\)'
t_RSHIFT = '>>'
t_RSHIFTEQUAL = '>>='
t_SEMI = ';'
t_STRING_LITERAL = '"([^"\\\\\\n]|(\\\\[0-9a-zA-Z._~!=&\\^\\-\\\\?\'"]))*"'
t_TIMES = '\\*'
t_TIMESEQUAL = '\\*='
t_UNMATCHED_QUOTE(t)[source]

Handles unmatched single-quote characters by raising an error.

t_XOR = '\\^'
t_XOREQUAL = '\\^='
t_error(t)[source]

Handles illegal character errors encountered during lexical analysis.

t_ignore = ' \t'
token()[source]
tokens = ('BOOL', 'BROADCAST', 'CLOCK', 'CHAN', 'CHAR', 'STRING', 'CONST', 'DO', 'DOUBLE', 'ELSE', 'FOR', 'IF', 'INT', 'RETURN', 'STRUCT', 'TYPEDEF', 'VOID', 'WHILE', 'ID', 'TYPEID', 'INT_CONST_DEC', 'DOUBLE_CONST', 'CHAR_CONST', 'BOOL_CONST', 'STRING_LITERAL', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'MOD', 'OR', 'AND', 'NOT', 'XOR', 'LSHIFT', 'RSHIFT', 'LOR', 'LAND', 'LNOT', 'LT', 'LE', 'GT', 'GE', 'EQ', 'NE', 'EQUALS', 'TIMESEQUAL', 'DIVEQUAL', 'MODEQUAL', 'PLUSEQUAL', 'MINUSEQUAL', 'LSHIFTEQUAL', 'RSHIFTEQUAL', 'ANDEQUAL', 'XOREQUAL', 'OREQUAL', 'ARROW', 'LPAREN', 'RPAREN', 'LBRACKET', 'RBRACKET', 'LBRACE', 'RBRACE', 'COMMA', 'PERIOD', 'SEMI', 'COLON')
unmatched_quote = '(\'([^\'\\\\\\n]|(\\\\(([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))|(\\d+)(?!\\d))))*\\n)|(\'([^\'\\\\\\n]|(\\\\(([a-wyzA-Z._~!=&\\^\\-\\\\?\'"]|x(?![0-9a-fA-F]))|(\\d+)(?!\\d))))*$)'

uppaal2jetracer.parser.declarationparser.declaration_parser module

This module defines the DeclarationParser class for parsing UPPAAL declarations and generating an Abstract Syntax Tree (AST). It leverages the PLY (Python Lex-Yacc) framework to build a lexer and parser, enabling the parsing of complex declaration rules and structures.

Based on the c_parser.py from the pycparser project by Eli Bendersky (https://eli.thegreenplace.net) under the BSD license.

Classes:
  • DeclarationParser: Main parser class responsible for processing UPPAAL declarations.

  • SingleStatementParser: Parser for single statements with relaxed rules on typing.

Key Features:
  • Manages scope information with a stack-based system for accurate type and identifier resolution.

  • Supports lexer and parser customization, as well as optimization for performance.

  • Includes utility methods for handling specific parsing scenarios, such as typedef management.

  • Provides detailed debugging capabilities, such as generating lexer and parser tables and debug outputs.

class DeclarationParser(lex_optimize=False, lexer=<class 'uppaal2jetracer.parser.declarationparser.declaration_lexer.DeclarationLexer'>, lextab='pycparser.lextab', yacc_optimize=False, yacctab='pycparser.yacctab', yacc_debug=False, taboutputdir='')[source]

Bases: PLYParser

Parses UPPAAL declarations and generates an Abstract Syntax Tree (AST).

This class is responsible for parsing code fom UPPAAL declarations, handling lexer and parser configurations, and managing scope information during parsing. It uses the PLY (Python Lex-Yacc) parser framework. It provides mechanisms to configure and optimize the lexer and parser, as well as utility methods for scope management and type lookup.

The class maintains a stack-based scope system to resolve identifiers and types, supports typedef name management, and facilitates interaction with the lexer and parser.

Variables:
  • dlex – The lexer used by the parser. Configured during the initialization phase.

  • tokens – List of tokens supported by the lexer for the parser.

  • _scope_stack – Maintains the stack of scopes for tracking symbols and types. Each scope is a dictionary of name-to-type mappings. A value of True indicates the name is a typedef; False means not a typedef.

  • _last_yielded_token – Stores the most recently seen token during parsing.

dlex
p_abstract_declarator_3(p)[source]

abstract_declarator : direct_abstract_declarator

p_abstract_declarator_opt(p)

abstract_declarator_opt : empty | abstract_declarator

p_argument_expression_list(p)[source]

argument_expression_list : assignment_expression | argument_expression_list COMMA assignment_expression

p_assignment_expression(p)[source]

assignment_expression : conditional_expression | unary_expression assignment_operator assignment_expression

p_assignment_expression_opt(p)

assignment_expression_opt : empty | assignment_expression

p_assignment_operator(p)[source]

assignment_operator : EQUALS | XOREQUAL | TIMESEQUAL | DIVEQUAL | MODEQUAL | PLUSEQUAL | MINUSEQUAL | LSHIFTEQUAL | RSHIFTEQUAL | ANDEQUAL | OREQUAL

p_binary_expression(p)[source]

binary_expression : unary_expression | binary_expression TIMES binary_expression | binary_expression DIVIDE binary_expression | binary_expression MOD binary_expression | binary_expression PLUS binary_expression | binary_expression MINUS binary_expression | binary_expression RSHIFT binary_expression | binary_expression LSHIFT binary_expression | binary_expression LT binary_expression | binary_expression LE binary_expression | binary_expression GE binary_expression | binary_expression GT binary_expression | binary_expression EQ binary_expression | binary_expression NE binary_expression | binary_expression AND binary_expression | binary_expression OR binary_expression | binary_expression XOR binary_expression | binary_expression LAND binary_expression | binary_expression LOR binary_expression

p_block_item(p)[source]

block_item : declaration | statement

p_block_item_list(p)[source]

block_item_list : block_item | block_item_list block_item

p_block_item_list_opt(p)

block_item_list_opt : empty | block_item_list

p_brace_close(p)[source]

brace_close : RBRACE

p_brace_open(p)[source]

brace_open : LBRACE

p_compound_statement_1(p)[source]

compound_statement : brace_open block_item_list_opt brace_close

p_conditional_expression(p)[source]

conditional_expression : binary_expression

p_constant_1(p)[source]

constant : INT_CONST_DEC

p_constant_2(p)[source]

constant : DOUBLE_CONST

p_constant_3(p)[source]

constant : CHAR_CONST

p_constant_4(p)[source]

constant : BOOL_CONST

p_constant_expression(p)[source]

constant_expression : conditional_expression

p_decl_body(p)[source]

decl_body : declaration_specifiers init_declarator_list_opt | declaration_specifiers_no_type id_init_declarator_list_opt

p_declaration(p)[source]

declaration : decl_body SEMI

p_declaration_list(p)[source]

declaration_list : declaration | declaration_list declaration

p_declaration_list_opt(p)

declaration_list_opt : empty | declaration_list

p_declaration_specifiers_1(p)[source]

declaration_specifiers : declaration_specifiers type_qualifier

p_declaration_specifiers_2(p)[source]

declaration_specifiers : declaration_specifiers TYPEDEF

p_declaration_specifiers_4(p)[source]

declaration_specifiers : declaration_specifiers type_specifier_no_typeid

p_declaration_specifiers_5(p)[source]

declaration_specifiers : type_specifier

p_declaration_specifiers_6(p)[source]

declaration_specifiers : declaration_specifiers_no_type type_specifier

p_declaration_specifiers_no_type_1(p)[source]

declaration_specifiers_no_type : type_qualifier declaration_specifiers_no_type_opt

p_declaration_specifiers_no_type_2(p)[source]

declaration_specifiers_no_type : TYPEDEF declaration_specifiers_no_type_opt

p_declaration_specifiers_no_type_opt(p)

declaration_specifiers_no_type_opt : empty | declaration_specifiers_no_type

p_declarator(p)[source]

declarator : id_declarator | typeid_declarator

p_designation(p)[source]

designation : designator_list EQUALS

p_designation_opt(p)

designation_opt : empty | designation

p_designator(p)[source]

designator : LBRACKET constant_expression RBRACKET | PERIOD identifier

p_designator_list(p)[source]

designator_list : designator | designator_list designator

p_direct_abstract_declarator_1(p)[source]

direct_abstract_declarator : LPAREN abstract_declarator RPAREN

p_direct_abstract_declarator_2(p)[source]

direct_abstract_declarator : direct_abstract_declarator LBRACKET assignment_expression_opt RBRACKET

p_direct_abstract_declarator_3(p)[source]

direct_abstract_declarator : LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET

p_direct_abstract_declarator_6(p)[source]

direct_abstract_declarator : direct_abstract_declarator LPAREN parameter_type_list_opt RPAREN

p_direct_abstract_declarator_7(p)[source]

direct_abstract_declarator : LPAREN parameter_type_list_opt RPAREN

p_direct_id_declarator_1(p)

direct_id_declarator : ID

p_direct_id_declarator_2(p)

direct_id_declarator : LPAREN id_declarator RPAREN

p_direct_id_declarator_3(p)

direct_id_declarator : direct_id_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET

p_direct_id_declarator_4(p)

direct_id_declarator : direct_id_declarator LBRACKET type_qualifier_list_opt assignment_expression RBRACKET | direct_id_declarator LBRACKET type_qualifier_list assignment_expression RBRACKET

p_direct_id_declarator_5(p)

direct_id_declarator : direct_id_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET

p_direct_id_declarator_6(p)

direct_id_declarator : direct_id_declarator LPAREN parameter_type_list RPAREN | direct_id_declarator LPAREN identifier_list_opt RPAREN

p_direct_id_declarator_7(p)

direct_id_declarator : LBRACKET assignment_expression_opt COMMA assignment_expression_opt RBRACKET direct_id_declarator

p_direct_typeid_declarator_1(p)

direct_typeid_declarator : TYPEID

p_direct_typeid_declarator_2(p)

direct_typeid_declarator : LPAREN typeid_declarator RPAREN

p_direct_typeid_declarator_3(p)

direct_typeid_declarator : direct_typeid_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET

p_direct_typeid_declarator_4(p)

direct_typeid_declarator : direct_typeid_declarator LBRACKET type_qualifier_list_opt assignment_expression RBRACKET | direct_typeid_declarator LBRACKET type_qualifier_list assignment_expression RBRACKET

p_direct_typeid_declarator_5(p)

direct_typeid_declarator : direct_typeid_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET

p_direct_typeid_declarator_6(p)

direct_typeid_declarator : direct_typeid_declarator LPAREN parameter_type_list RPAREN | direct_typeid_declarator LPAREN identifier_list_opt RPAREN

p_direct_typeid_declarator_7(p)

direct_typeid_declarator : LBRACKET assignment_expression_opt COMMA assignment_expression_opt RBRACKET direct_typeid_declarator

p_direct_typeid_noparen_declarator_1(p)

direct_typeid_noparen_declarator : TYPEID

p_direct_typeid_noparen_declarator_3(p)

direct_typeid_noparen_declarator : direct_typeid_noparen_declarator LBRACKET type_qualifier_list_opt assignment_expression_opt RBRACKET

p_direct_typeid_noparen_declarator_4(p)

direct_typeid_noparen_declarator : direct_typeid_noparen_declarator LBRACKET type_qualifier_list_opt assignment_expression RBRACKET | direct_typeid_noparen_declarator LBRACKET type_qualifier_list assignment_expression RBRACKET

p_direct_typeid_noparen_declarator_5(p)

direct_typeid_noparen_declarator : direct_typeid_noparen_declarator LBRACKET type_qualifier_list_opt TIMES RBRACKET

p_direct_typeid_noparen_declarator_6(p)

direct_typeid_noparen_declarator : direct_typeid_noparen_declarator LPAREN parameter_type_list RPAREN | direct_typeid_noparen_declarator LPAREN identifier_list_opt RPAREN

p_direct_typeid_noparen_declarator_7(p)

direct_typeid_noparen_declarator : LBRACKET assignment_expression_opt COMMA assignment_expression_opt RBRACKET direct_typeid_noparen_declarator

p_empty(p)[source]

empty :

p_error(p)[source]

Throws a ParseError.

p_expression(p)[source]

expression : assignment_expression | expression COMMA assignment_expression

p_expression_opt(p)

expression_opt : empty | expression

p_expression_statement(p)[source]

expression_statement : expression_opt SEMI

p_external_declaration_1(p)[source]

external_declaration : function_definition

p_external_declaration_2(p)[source]

external_declaration : declaration

p_external_declaration_3(p)[source]

external_declaration : SEMI

p_function_definition(p)[source]

function_definition : declaration_specifiers id_declarator declaration_list_opt compound_statement

p_id_declarator_1(p)

id_declarator : direct_id_declarator

p_id_init_declarator(p)[source]

id_init_declarator : id_declarator | id_declarator EQUALS initializer

p_id_init_declarator_list(p)[source]

id_init_declarator_list : id_init_declarator | id_init_declarator_list COMMA init_declarator

p_id_init_declarator_list_opt(p)

id_init_declarator_list_opt : empty | id_init_declarator_list

p_identifier(p)[source]

identifier : ID

p_identifier_list(p)[source]

identifier_list : identifier | identifier_list COMMA identifier

p_identifier_list_opt(p)

identifier_list_opt : empty | identifier_list

p_init_declarator(p)[source]

init_declarator : declarator | declarator EQUALS initializer

p_init_declarator_list(p)[source]

init_declarator_list : init_declarator | init_declarator_list COMMA init_declarator

p_init_declarator_list_opt(p)

init_declarator_list_opt : empty | init_declarator_list

p_initializer_1(p)[source]

initializer : assignment_expression

p_initializer_2(p)[source]

initializer : brace_open initializer_list_opt brace_close | brace_open initializer_list COMMA brace_close

p_initializer_list(p)[source]

initializer_list : designation_opt initializer | initializer_list COMMA designation_opt initializer

p_initializer_list_opt(p)

initializer_list_opt : empty | initializer_list

p_iteration_statement_1(p)[source]

iteration_statement : WHILE LPAREN expression RPAREN statement

p_iteration_statement_2(p)[source]

iteration_statement : DO statement WHILE LPAREN expression RPAREN SEMI

p_iteration_statement_3(p)[source]

iteration_statement : FOR LPAREN expression_opt SEMI expression_opt SEMI expression_opt RPAREN statement

p_iteration_statement_4(p)[source]

iteration_statement : FOR LPAREN declaration expression_opt SEMI expression_opt RPAREN statement

p_jump_statement(p)[source]

jump_statement : RETURN expression SEMI | RETURN SEMI

p_parameter_declaration_1(p)[source]

parameter_declaration : declaration_specifiers id_declarator | declaration_specifiers typeid_noparen_declarator

p_parameter_declaration_2(p)[source]

parameter_declaration : declaration_specifiers abstract_declarator_opt

p_parameter_list(p)[source]

parameter_list : parameter_declaration | parameter_list COMMA parameter_declaration

p_parameter_type_list(p)[source]

parameter_type_list : parameter_list

p_parameter_type_list_opt(p)

parameter_type_list_opt : empty | parameter_type_list

p_parenthesized_compound_expression(p)[source]

assignment_expression : LPAREN compound_statement RPAREN

p_postfix_expression_1(p)[source]

postfix_expression : primary_expression

p_postfix_expression_2(p)[source]

postfix_expression : postfix_expression LBRACKET expression RBRACKET

p_postfix_expression_3(p)[source]

postfix_expression : postfix_expression LPAREN argument_expression_list RPAREN | postfix_expression LPAREN RPAREN

p_postfix_expression_4(p)[source]

postfix_expression : postfix_expression PERIOD ID | postfix_expression PERIOD TYPEID | postfix_expression ARROW ID | postfix_expression ARROW TYPEID

p_postfix_expression_6(p)[source]

postfix_expression : LPAREN type_name RPAREN brace_open initializer_list brace_close | LPAREN type_name RPAREN brace_open initializer_list COMMA brace_close

p_primary_expression_1(p)[source]

primary_expression : identifier

p_primary_expression_2(p)[source]

primary_expression : constant

p_primary_expression_3(p)[source]

primary_expression : unified_string_literal

p_primary_expression_4(p)[source]

primary_expression : LPAREN expression RPAREN

p_selection_statement_1(p)[source]

selection_statement : IF LPAREN expression RPAREN statement

p_selection_statement_2(p)[source]

selection_statement : IF LPAREN expression RPAREN statement ELSE statement

p_specifier_qualifier_list_1(p)[source]

specifier_qualifier_list : specifier_qualifier_list type_specifier_no_typeid

p_specifier_qualifier_list_2(p)[source]

specifier_qualifier_list : specifier_qualifier_list type_qualifier

p_specifier_qualifier_list_3(p)[source]

specifier_qualifier_list : type_specifier

p_specifier_qualifier_list_4(p)[source]

specifier_qualifier_list : type_qualifier_list type_specifier

p_statement(p)[source]

statement : expression_statement | compound_statement | selection_statement | iteration_statement | jump_statement

p_struct_declaration_1(p)[source]

struct_declaration : specifier_qualifier_list struct_declarator_list_opt SEMI

p_struct_declaration_2(p)[source]

struct_declaration : SEMI

p_struct_declaration_list(p)[source]

struct_declaration_list : struct_declaration | struct_declaration_list struct_declaration

p_struct_declarator(p)[source]

struct_declarator : declarator

p_struct_declarator_list(p)[source]

struct_declarator_list : struct_declarator | struct_declarator_list COMMA struct_declarator

p_struct_declarator_list_opt(p)

struct_declarator_list_opt : empty | struct_declarator_list

p_struct_specifier_1(p)[source]

struct_specifier : STRUCT ID | STRUCT TYPEID

p_struct_specifier_2(p)[source]

struct_specifier : STRUCT brace_open struct_declaration_list brace_close | STRUCT brace_open brace_close

p_struct_specifier_3(p)[source]

struct_specifier : STRUCT ID brace_open struct_declaration_list brace_close | STRUCT ID brace_open brace_close | STRUCT TYPEID brace_open struct_declaration_list brace_close | STRUCT TYPEID brace_open brace_close

p_translation_unit_1(p)[source]

translation_unit : external_declaration

p_translation_unit_2(p)[source]

translation_unit : translation_unit external_declaration

p_translation_unit_or_empty(p)[source]

translation_unit_or_empty : translation_unit | empty

p_type_name(p)[source]

type_name : specifier_qualifier_list abstract_declarator_opt

p_type_qualifier(p)[source]

type_qualifier : CONST | BROADCAST

p_type_qualifier_list(p)[source]

type_qualifier_list : type_qualifier | type_qualifier_list type_qualifier

p_type_qualifier_list_opt(p)

type_qualifier_list_opt : empty | type_qualifier_list

p_type_specifier(p)[source]

type_specifier : typedef_name | struct_specifier | type_specifier_no_typeid

p_type_specifier_no_typeid(p)[source]

type_specifier_no_typeid : VOID | BOOL | CHAN | CHAR | STRING | CLOCK | INT | DOUBLE

p_typedef_name(p)[source]

typedef_name : TYPEID

p_typeid_declarator_1(p)

typeid_declarator : direct_typeid_declarator

p_typeid_noparen_declarator_1(p)

typeid_noparen_declarator : direct_typeid_noparen_declarator

p_unary_expression_1(p)[source]

unary_expression : postfix_expression

p_unary_expression_2(p)[source]

unary_expression : unary_operator unary_expression

p_unary_operator(p)[source]

unary_operator : AND | TIMES | PLUS | MINUS | NOT | LNOT

p_unified_string_literal(p)[source]

unified_string_literal : STRING_LITERAL | unified_string_literal STRING_LITERAL

parse(text, filename='', debug=False)[source]

Parses C code and returns an AST.

text:

A string containing the C source code

filename:

Name of the file being parsed (for meaningful error messages)

debug:

Debug flag to YACC

precedence = (('left', 'LOR'), ('left', 'LAND'), ('left', 'OR'), ('left', 'XOR'), ('left', 'AND'), ('left', 'EQ', 'NE'), ('left', 'GT', 'GE', 'LT', 'LE'), ('left', 'RSHIFT', 'LSHIFT'), ('left', 'PLUS', 'MINUS'), ('left', 'TIMES', 'DIVIDE', 'MOD'))
class SingleStatementParser(*args, **kwargs)[source]

Bases: DeclarationParser

SingleStatementParser class.

Parses single expressions or statements (without requiring a semicolon or declared variables). Supports simple assignments, function calls, and expressions.

Ensures the root node of the generated AST is always a FileAST.

dlex
p_abstract_declarator_opt(p)

abstract_declarator_opt : empty | abstract_declarator

p_assignment_expression_opt(p)

assignment_expression_opt : empty | assignment_expression

p_block_item_list_opt(p)

block_item_list_opt : empty | block_item_list

p_declaration_list_opt(p)

declaration_list_opt : empty | declaration_list

p_declaration_specifiers_no_type_opt(p)

declaration_specifiers_no_type_opt : empty | declaration_specifiers_no_type

p_designation_opt(p)

designation_opt : empty | designation

p_expression_opt(p)

expression_opt : empty | expression

p_expression_statement(p)[source]

expression_statement : expression

p_external_declaration_6(p)[source]

external_declaration : expression_statement

p_id_init_declarator_list_opt(p)

id_init_declarator_list_opt : empty | id_init_declarator_list

p_identifier_list_opt(p)

identifier_list_opt : empty | identifier_list

p_init_declarator_list_opt(p)

init_declarator_list_opt : empty | init_declarator_list

p_initializer_list_opt(p)

initializer_list_opt : empty | initializer_list

p_parameter_type_list_opt(p)

parameter_type_list_opt : empty | parameter_type_list

p_single_line_expression(p)[source]

single_line_expression : expression

p_struct_declarator_list_opt(p)

struct_declarator_list_opt : empty | struct_declarator_list

p_type_qualifier_list_opt(p)

type_qualifier_list_opt : empty | type_qualifier_list

uppaal2jetracer.parser.declarationparser.plyparser module

This module contains the implementation of a PLY-based parser utility designed to aid in constructing and using parsers. It includes classes for representing coordinates of syntax elements (Coord), custom exception handling (ParseError), and a PLYParser class that provides helper methods for rule creation, error handling, and token coordinate computation.

Additionally, the module defines utilities for dynamic rule generation, including parameterized rule decorators (parameterized) and a class decorator (template) to facilitate the creation of PLY rules from templates.

Based on the plyparser.py from the pycparser module by Eli Bendersky (https://eli.thegreenplace.net) under the BSD license.

Key Components:
  • Coord: Represents the file, line, and optionally column for syntax elements.

  • PLYParser: Provides methods to handle optional rules, error reporting, and coordinate management for tokens during parsing.

  • parameterized: Decorator for creating parameterized parsing rules.

  • template: Class decorator for generating rules from parameterized templates.

This module can be used as part of a larger system to build a parser using the PLY (Python Lex-Yacc) library.

class Coord(file, line, column=None)[source]

Bases: object

Coordinates of a syntactic element. Consists of: :ivar file: Name of the file. :type file: str :ivar line: The line number of the coordinate in the input string :type line: int :ivar column: The column number of the coordinate in the input string :type column: int

column
file
line
class PLYParser[source]

Bases: object

Facilitates parsing tasks with helper methods and error handling specifically designed to work with the PLY parsing library. This class aids in managing parsing rules, tracking coordinates during parsing, and generating useful error messages for syntax issues.

Variables:

dlex – Lexer instance providing input and tokens for parsing.

dlex
exception ParseError[source]

Bases: Exception

Thrown if yacc has an internal problem or by PLYParser if a token cannot be parsed.

parameterized(*params)[source]

Decorator to create parameterized rules.

template(cls)[source]

Class decorator to generate rules from parameterized rule templates.

See parameterized for more information on parameterized rules.

Module contents