edubart / lpegrex Goto Github PK
View Code? Open in Web Editor NEWParse programming languages syntax into an AST using PEGs with ease (LPeg Extension).
License: MIT License
Parse programming languages syntax into an AST using PEGs with ease (LPeg Extension).
License: MIT License
Testing the C11 parser with the preprocessed sqlite3.c produces a complete AST for it.
But when trying to parse the valid C11 fragment shown bellow from this paper https://hal.archives-ouvertes.fr/hal-01633123/document gives this error:
lua examples/c11-ast.lua inputs/namespaces.c
lua: ./parsers/c11.lua:672: syntax error: inputs/namespaces.c:9:6: Expected_;
T: s.T = 2;
^
stack traceback:
[C]: in function 'error'
./parsers/c11.lua:672: in function 'parsers.c11'
examples/c11-ast.lua:15: in main chunk
[C]: in ?
// namespaces.c
typedef int S, T, U;
struct S { int T; };
union U { int x; };
void f(void) {
// The following uses of S, T, U are correct, and have no
// effect on the visibility of S, T, U as typedef names.
struct S s = { .T = 1 };
T: s.T = 2;
union U u = { 1 };
goto T;
// S, T and U are still typedef names:
S ss = 1; T tt = 1; U uu = 1;
}
Testing the Lua parser examples/lua-ast.lua
with this input:
-- Convert an AST into a human readable string.
function lpegrex.prettyast(node)
local ss = {}
ast2string(node '', ss) -- !!! notice here I removed the comma between node and ''
ss[#ss+1] = indent..tostring(child)
return table.concat(ss, '\n')
end
return lpegrex
Output:
lua examples/lua-ast.lua /home/mingo/dev/c/A_grammars/peg-dad/dad/lua-test-return.lua
Block
| FuncDef
| | DotIndex
| | | "prettyast"
| | | Id
| | | | "lpegrex"
| | -
| | | Id
| | | | "node"
| | Block
| | | VarDecl
| | | | -
| | | | | IdDecl
| | | | | | "ss"
| | | | -
| | | | | Table
| | | Call
| | | | -
| | | | | Call
| | | | | | -
| | | | | | | String
| | | | | | | | ""
| | | | | | Id
| | | | | | | "node"
| | | | | Id
| | | | | | "ss"
| | | | Id
| | | | | "ast2string"
| | | Assign
| | | | -
| | | | | KeyIndex
| | | | | | BinaryOp
| | | | | | | UnaryOp
| | | | | | | | "len"
| | | | | | | | Id
| | | | | | | | | "ss"
| | | | | | | "add"
| | | | | | | Number
| | | | | | | | 1
| | | | | | Id
| | | | | | | "ss"
| | | | -
| | | | | BinaryOp
| | | | | | Id
| | | | | | | "indent"
| | | | | | "concat"
| | | | | | Call
| | | | | | | -
| | | | | | | | Id
| | | | | | | | | "child"
| | | | | | | Id
| | | | | | | | "tostring"
| | | Return
| | | | -
| | | | | Call
| | | | | | -
| | | | | | | Id
| | | | | | | | "ss"
| | | | | | | String
| | | | | | | | "\n"
| | | | | | DotIndex
| | | | | | | "concat"
| | | | | | | Id
| | | | | | | | "table"
| Return
| | -
| | | Id
| | | | "lpegrex"
While trying to create a parser using lpegrex
and looking at C11
parser for usage patterns I found the one shown bellow, where most of the binary operands are captured with {`op`}
but the AND
operator is captured with {'&&'}
is this intentional or a mistake ?
logical-AND-expression <--
(inclusive-OR-expression op-logical-AND*) ~> foldleft
op-logical-AND:binary-op <==
{'&&'} SKIP @inclusive-OR-expression
logical-OR-expression <--
(logical-AND-expression op-logical-OR*) ~> foldleft
op-logical-OR:binary-op <==
{`||`} @logical-AND-expression
Would be nice to have a lpegrex
grammar parser as example, I've got this one grammar so far:
-- lpegrex in lpeg
pattern <- exp !.
exp <- S (grammar / alternative)
alternative <- seq ('/' S seq)*
seq <- prefix*
prefix <- '&' S prefix / '!' S prefix / suffix
suffix <- primary S (([+*?] S
/ '^' (
[-+]? num -- (exactly n / at (least / most) n repetitions
/ '^' name -- Syntax of relabel module. The pattern p^l is equivalent to p + lpeglabel.T(l).
)
/ '->' S (string / pcap / name / num) -- capture
/ '=>' S name) S)* -- match time capture
primary <- '(' exp ')' S / string / keyword / class / defined
/ '{:' (name ':')? exp ':}' S -- (named or anonymous) group capture
/ '=' name -- back reference
/ '@' exp -- for throwing labels errors on failure of expected matches
/ pcap
/ '{~' exp '~}' S -- substitution capture
/ '{|' exp '|}' S -- table capture
/ '{' exp '}' S -- simple capture
/ '~?' S -- optional capture
/ '~>' S ( 'foldleft' / 'foldright' / 'rfoldleft' / 'rfoldright' / name ) S -- fold capture
/ '$' (string / name / num / pcap)
/ '.' S
/ name S !(asttag / arrow )
/ '<' S name '>' S -- old-style non terminals
/ '%{' S name '}' S -- Syntax of relabel module. Equivalent to lpeglabel.T(l)
grammar <- definition+
definition <- name S asttag? arrow exp
class <- '[' '^'? item (!']' item)* ']' S
item <- defined / range / .
range <- . '-' [^]]
S <- (%s / '--' [^%nl]*)* -- spaces and comments
name <- [A-Za-z_]([A-Za-z0-9_] / '-' !'>' )* S
arrow <- ( '<--' -- rule
/ '<==' -- for rules that capture AST Nodes
/ '<-|' -- for rules that capture tables
/ '<-'
) S
num <- [0-9]+ S
string <- ('"' [^"]* '"' / "'" [^']* "'") S
defined <- '%' name -- pattern defs[name] or a pre-defined pattern
keyword <- '`' [^`]+ '`' S -- tokens/keywords with automatic skipping
asttag <- ':' S name -- Capture tagged node rule
pcap <- '{}' S -- position capture
But it doesn't do any capture to genereate an AST.
Can someone help finish it ?
The option "tag" is very useful, and right now it is not documented... suggestion: mention it in the docs! The test that uses it in tests/lpegrex-test.lua is a good example of usage.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.