Commit e30f7617 authored by Igor Dejanovic's avatar Igor Dejanovic

Fixing peg_peg example

Reworked peg_peg example to align with the previous changes.
parent 94958f52
...@@ -16,8 +16,7 @@ from arpeggio.peg import ParserPEG ...@@ -16,8 +16,7 @@ from arpeggio.peg import ParserPEG
# Semantic actions # Semantic actions
from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence,\ from arpeggio.peg import SemGrammar, SemRule, SemOrderedChoice, SemSequence,\
SemPrefix, SemSufix, SemExpression, SemRegEx, SemIdentifier, SemLiteral,\ SemPrefix, SemSufix, SemExpression, SemRegEx, SemStrMatch, SemRuleCrossRef
SemTerminal
sem_actions = { sem_actions = {
"grammar": SemGrammar(), "grammar": SemGrammar(),
...@@ -28,29 +27,26 @@ sem_actions = { ...@@ -28,29 +27,26 @@ sem_actions = {
"sufix": SemSufix(), "sufix": SemSufix(),
"expression": SemExpression(), "expression": SemExpression(),
"regex": SemRegEx(), "regex": SemRegEx(),
"identifier": SemIdentifier(), "str_match": SemStrMatch(),
"literal": SemLiteral() "rule_crossref": SemRuleCrossRef()
} }
for sem in ["LEFT_ARROW", "SLASH", "STAR", "QUESTION", "PLUS", "AND", "NOT",
"OPEN", "CLOSE"]:
sem_actions[sem] = SemTerminal()
# PEG defined using PEG itself. # PEG defined using PEG itself.
peg_grammar = r""" peg_grammar = r"""
grammar <- rule+ EndOfFile; grammar <- rule+ EOF;
rule <- identifier LEFT_ARROW ordered_choice ';'; rule <- rule_name LEFT_ARROW ordered_choice ';';
ordered_choice <- sequence (SLASH sequence)*; ordered_choice <- sequence (SLASH sequence)*;
sequence <- prefix+; sequence <- prefix+;
prefix <- (AND/NOT)? sufix; prefix <- (AND/NOT)? sufix;
sufix <- expression (QUESTION/STAR/PLUS)?; sufix <- expression (QUESTION/STAR/PLUS)?;
expression <- regex / (identifier !LEFT_ARROW) expression <- regex / rule_crossref
/ ("(" ordered_choice ")") / literal; / (OPEN ordered_choice CLOSE) / str_match;
identifier <- r'[a-zA-Z_]([a-zA-Z_]|[0-9])*'; rule_name <- r'[a-zA-Z_]([a-zA-Z_]|[0-9])*';
rule_crossref <- rule_name;
regex <- 'r\'' r'(\\\'|[^\'])*' '\''; regex <- 'r\'' r'(\\\'|[^\'])*' '\'';
literal <- r'\'(\\\'|[^\'])*\'|"[^"]*"'; str_match <- r'\'(\\\'|[^\'])*\'|"[^"]*"';
LEFT_ARROW <- '<-'; LEFT_ARROW <- '<-';
SLASH <- '/'; SLASH <- '/';
AND <- '&'; AND <- '&';
...@@ -78,7 +74,7 @@ PMDOTExporter().exportFile(parser.parser_model, ...@@ -78,7 +74,7 @@ PMDOTExporter().exportFile(parser.parser_model,
# using PEG itself. # using PEG itself.
parser.parse(peg_grammar) parser.parse(peg_grammar)
# Again we export parse tree in dot file for vizualization. # Again we export parse tree in dot file for visualization.
PTDOTExporter().exportFile(parser.parse_tree, PTDOTExporter().exportFile(parser.parse_tree,
"peg_peg_parse_tree.dot") "peg_peg_parse_tree.dot")
...@@ -95,4 +91,3 @@ PMDOTExporter().exportFile(asg, ...@@ -95,4 +91,3 @@ PMDOTExporter().exportFile(asg,
# parse PEG grammars # parse PEG grammars
parser.parser_model = asg parser.parser_model = asg
parser.parse(peg_grammar) parser.parse(peg_grammar)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment