Re: Examples or tutorials for Perl grammars?
by Fletch (Bishop) on Jul 09, 2021 at 04:45 UTC
|
| [reply] |
Re: Examples or tutorials for Perl grammars?
by LanX (Sage) on Jul 09, 2021 at 08:46 UTC
|
It depends what you want to achieve. Could you be more specific?
There are various modules on CPAN to parse SQL text like
If it's just about creating your own SQL DSL , I'd suggest designing a combination of Perl functions like sub SELECT and overloaded operators to return an AST of your queries.
I did this before... °
update
and of course there is also
°) RFC: A DSL for SQL (part 1) | [reply] [d/l] |
|
«… I did this before...»
Any example available for free? No kidding. Best regards, Karl
BTW, I wonder why someone jumps to the conclusion to build an SQLish language. Isn’t SQL bad enough? The world mapped to index cards 🤪
«The Crux of the Biscuit is the Apostrophe»
| [reply] |
|
| [reply] |
|
|
| [reply] |
|
|
|
|
|
| [reply] |
|
Abstract Syntax Tree. That's the structure parsers usually return (but they don't have to, see e.g. my Marpa Enhanced Calculator where the parser directly calculates the expressions without actually building the tree).
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] |
|
| [reply] [d/l] |
|
Thanks for that, although it's kind ot taken the question off on a tangent.
It's not a database language or directly related to SQL. That was just an example.
| [reply] |
|
| [reply] |
|
| [reply] |
Re: Examples or tutorials for Perl grammars?
by tybalt89 (Monsignor) on Jul 09, 2021 at 23:29 UTC
|
A good start would be to write a few (short) example programs in your new language and show them here to give the rest of us an idea of what you want to do.
Also save them for later as test cases (hint: to validate a language usually requires lots and lots of test cases :).
Also try to generate an EBNF for your language and show that here as well.
Parsers are not (usually) one huge regex, but a set of smaller regexes with surrounding code that call each other recursively (sort of, maybe, perhaps...).
| [reply] |
Re: Examples or tutorials for Perl grammars?
by perlfan (Vicar) on Jul 09, 2021 at 05:36 UTC
|
If it's a bit like SQL, then you'll save yourself a lot of trouble if you can describe first your toy language as a BNF; here's on for SQL-92. From there, sure there are modules that will allow you to create a parser for it. But the question then becomes, what are you going to do once you get the abstract syntax tree (AST), which is what you get once it's "parsed"?
Mind sharing a comprehensive example of usage? It may be that you don't need to invent your own DSL, and what you really need are just a few well chose keywords implemented as a subset of Perl (or perhaps creative use of prototype)?
FWIW, the approach to create your own set of DSL keywords is likely more expedient and easier than literally creating your own "language". Did you know Lua started off as a way to describe data? It's also what's used in programs like nmap to implement it's own DSL. Similarly, mysqlproxy uses it. I know you said you need to stick with Perl 5, but Inline::Lua might help. | [reply] [d/l] |
Re: Examples or tutorials for Perl grammars?
by tybalt89 (Monsignor) on Jul 28, 2021 at 21:07 UTC
|
With no example of "something like that" and a desire to continue my policy of "always provide code",
here's a "tinylanguage" parser complete with AST interpreter and the ability to translate to an equivalent perl program.
Tinylanguage is similar to but slightly different (postfix 'while', ?: instead of 'if') from
https://rosettacode.org/wiki/Compiler/syntax_analyzer
which is a fairly good example also (see the perl code).
The AST consists of perl objects and is displayed in a hierarchical indentation form.
The generated perl is weird, but as far as I have tested it, correct (TIMTOWTDI).
which outputs:
I hope this helps and give you some idea of what's involved in parsing a language.
| [reply] [d/l] [select] |
A reply falls below the community's threshold of quality. You may see it by logging in. |