background preloader

Write Interpreter

Facebook Twitter

CS 61B Lecture 37: Expression Parsing. Parsing expression grammar. Unlike CFGs, PEGs cannot be ambiguous; if a string parses, it has exactly one valid parse tree.

Parsing expression grammar

It is conjectured that there exist context-free languages that cannot be parsed by a PEG, but this is not yet proven.[1] PEGs are well-suited to parsing computer languages, but not natural languages where their performance is comparable to general CFG algorithms such as the Earley algorithm.[2] Definition[edit] Syntax[edit] Formally, a parsing expression grammar consists of: A finite set N of nonterminal symbols.A finite set Σ of terminal symbols that is disjoint from N.A finite set P of parsing rules.An expression eS termed the starting expression.

An atomic parsing expression consists of: any terminal symbol,any nonterminal symbol, orthe empty string ε.Given any existing parsing expressions e, e1, and e2, a new parsing expression can be constructed using the following operators: Sequence: e1 e2Ordered choice: e1 / e2Zero-or-more: e*One-or-more: e+Optional: e? Semantics[edit] Examples[edit] . JFlex - User's Manual. The Fast Lexical Analyser Generator Copyright ©1998-2014 by Gerwin Klein, Steve Rowe, and Régis Décamps.

JFlex - User's Manual

JFlex User's Manual Version 1.5.1, March 21, 2014 Contents JFlex is a lexical analyser generator for Java written in Java. Design goals The main design goals of JFlex are: Full unicode supportFast generated scanners Fast scanner generationConvenient specification syntaxPlatform independenceJLex compatibility About this manual This manual gives a brief but complete description of the tool JFlex. The next section of this manual describes installation procedures for JFlex. Installing JFlex Windows To install JFlex on Windows 95/98/NT/XP, follow these three steps: Unzip the file you downloaded into the directory you want JFlex in (using something like WinZip). Unix with tar archive. Syntactic_recognition. Treetop grammars are written in a custom language based on parsing expression grammars.

syntactic_recognition

Literature on the subject of parsing expression grammars (PEGs) is useful in writing Treetop grammars. PEGs have no separate lexical analyser (since the algorithm has the same time-complexity guarantees as the best lexical analysers) so all whitespace and other lexical niceties (like comments) must be explicitly handled in the grammar. A further benefit is that multiple PEG grammars may be seamlessly composed into a single parser. Treetop grammars look like this: require "my_stuff" grammar GrammarName include Module::Submodule rule rule_name ... end rule rule_name ... end ... end The main keywords are: grammar : This introduces a new grammar. A grammar may be surrounded by one or more nested module statements, which provides a namespace for the generated Ruby parser. Treetop will emit a module called GrammarName and a parser class called GrammarNameParser (in the module namespace, if specified).

The If Works - Talk: Writing a language in 15 minutes.