Module parser :: Class LRparser
[hide private]
[frames] | no frames]

Class LRparser

source code

Known Subclasses:

Class for LR parser

Instance Methods [hide private]
 
__init__(self, grammar, table_shelve, no_table=1, tabletype=<class parser.LALRtable at 0x15c9b90>, operators=None, noconflicts=1, expect=0, **args) source code
 
__str__(self)
Returns: the LR parsing table showing for each state the action and goto function
source code
 
parsing(self, tokens, context=None)
LR Parsing Algorithm (aho86:_compil, page 218)
source code
 
parse_grammar(self, st, context, args)
Transforms a string into a grammar description
source code
 
gsrules(self, rulestr, **sym)
Transforms a string in a grammar description
source code
Instance Variables [hide private]
  ACTION
Action function
  GOTO
Goto function
  cfgr
context free grammar
  context
computational context
  nonterminals
grammar nonterminals
  output
list of grammar rules used for parsing tokens (right derivation in reverse)
  rules
grammar rules
  stack
LR stack with pairs (state,token)
  table
LR parsing table
  terminals
grammar terminals
  tokens
tokens to be parsed
Method Details [hide private]

__init__(self, grammar, table_shelve, no_table=1, tabletype=<class parser.LALRtable at 0x15c9b90>, operators=None, noconflicts=1, expect=0, **args)
(Constructor)

source code 
Parameters:
  • grammar - is a list for productions; each production is a tuple (LeftHandside,RightHandside,SemFunc,Prec?) with LeftHandside nonterminal, RightHandside list of symbols, SemFunc syntax-direct semantics, if present Prec (PRECEDENCE,ASSOC) for ambiguous rules

    First production is for start symbol

  • table_shelve (string) - file where parser is saved
  • tabletype (LRtable class) - type of LR table: SLR, LR1, LALR
  • no_table (integer) - if 0 table_shelve is created anyway
  • operators (dictionary) - precedence and associativity for operators
  • noconflicts (integer) - if 0 LRtable conflicts are not resolved, unless spcecial operator rules
  • expect (integer) - exact number of expected LR shift/reduce conflicts
  • args (dictionary) - extra arguments; key nosemrules if 1 no semantic rules are applied

__str__(self)
(Informal representation operator)

source code 
Returns:
the LR parsing table showing for each state the action and goto function

parsing(self, tokens, context=None)

source code 

LR Parsing Algorithm (aho86:_compil, page 218)

Parameters:
  • tokens - pairs (TOKEN, SPECIAL_VALUE)
  • context - a computational context for semantic actions
Returns:
parsed result

parse_grammar(self, st, context, args)

source code 

Transforms a string into a grammar description

Parameters:
  • st - is a string representing the grammar rules, with default symbols as below. Fisrt rule for start.

    Example:

        reg -> reg + reg E{lb}E{lb} self.OrSemRule   E{rb}E{rb}
        // priority 'left'|
                    ( reg ) E{lb}E{lb}self.ParSemRuleE{rb}E{rb} ;
    

    where:

    • rulesym="->" production symbol
    • rhssep='' RHS symbols separator
    • opsym='//' operator definition separator
    • semsym={{ semantic rule start marker
    • csemsym=}} semantic rule end marker
    • rulesep='|' separator for multiple rules for a LHS
    • ruleend=';' end marker for one LHS rule

gsrules(self, rulestr, **sym)

source code 

Transforms a string in a grammar description

Parameters:
  • rulestr - is a string representing the grammar rules, with default symbols as below.
  • sym - Dictionary with symbols used. Default ones:
    • rulesym="->" production symbol
    • rhssep='' RHS symbols separator
    • opsym='//' operator definition separator
    • semsym={{ semantic rule start marker
    • csemsym=}} semantic rule end marker
    • rulesep='|' separator for multiple rules for a LHS
    • ruleend=';' end marker for one LHS rule Example: reg -> reg + reg {{ self.OrSemRule // (priority,'left') }} | ( reg ) {{self.ParSemRule}} ;