PEGs

Apuntes de PEG.js

Repositorios

Herramientas

Bactracking en los PEGs

!INCLUDE "pegjsbacktracking.md"

Ejemplos

> parser = PEG.buildParser(`d = 'c'\ns = ('a' / 'b')+\n`, {allowedStartRules: ['d', 's']})
{ SyntaxError: [Function: peg$SyntaxError],
  parse: [Function: peg$parse] }
> r = parser.parse('c')
'c'
> r = parser.parse('aba', {startRule: "s"})
[ 'a', 'b', 'a' ]
> parser = PEG.buildParser(`d = 'c'\ns = ('a' / 'b')+\n`, {allowedStartRules: ['d', 's'], output: "source"}); typeof parser
'string'
[~/pegjs/examples(master)]$ cat initializer.js
"use strict";
const PEG = require("pegjs");
const grammar = `
 {                             
   const util = require("util");     

   var g = "visible variable"; 
   console.log("Inside Initializer! options = "+util.inspect(options)); 
 }                             
 start = 'a' { console.log('g = '+g); return 1; } 
       /  &  { console.log('inside predicate: g = '+g); return true; } 'b' { return 2; }
`;

console.log("GRAMMAR:\n"+grammar);

const parser = PEG.buildParser(grammar);

console.log("PARSING 'a'");
let r = parser.parse("a", { x: 'hello' });
console.log(r); 
console.log("PARSING 'b'");
r = parser.parse("b");
console.log(r);

Patrick Dubroy: Parsing, Compiling, and Static Metaprogramming

Tree Transformations

Borrador

  • borrador (work in progress. Don't look here)

results matching ""

    No results matching ""