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

Class LRparser

Known Subclasses:
Yappy

Class for LR parser
Method Summary
  __init__(self, grammar, table_shelve, no_table, tabletype, operators, noconflicts, expect, **args)
  __str__(self)
Return the LR parsing table showing for each state the action and goto function
  gsrules(self, rulestr, **sym)
Transforms a string in a grammar description
  parse_grammar(self, st, context)
Transforms a string into a grammar description
  parsing(self, tokens, context)
LR Parsing Algorithm (aho86:_compil, page 218)

Instance Variable Summary
  ACTION: Action function
  cfgr: context free grammar
  context: computational context
  GOTO: Goto function
  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

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

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 - file where parser is saved
           (type=string)
no_table - if 0 table_shelve is created anyway
           (type=integer)
tabletype - type of LR table: SLR, LR1, LALR
           (type=LRtable class)
operators - precedence and associativity for operators
           (type=dictionary)
noconflicts - if 0 LRtable conflicts are not resolved, unless spcecial operator rules
           (type=integer)
expect - exact number of expected LR shift/reduce conflicts
           (type=integer)

__str__(self)
(Informal representation operator)

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

gsrules(self, rulestr, **sym)

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}} ;

parse_grammar(self, st, context)

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 // (priority,'left') E{rb}E{rb} |
                ( 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

parsing(self, tokens, context=None)

LR Parsing Algorithm (aho86:_compil, page 218)
Parameters:
tokens - pairs (TOKEN, SPECIAL_VALUE)
context - a computational context for semantic actions
Returns:
parsed result

Instance Variable Details

ACTION

Action function

cfgr

context free grammar

context

computational context

GOTO

Goto function

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

Generated by Epydoc 2.1 on Mon Mar 29 22:42:43 2004 http://epydoc.sf.net