Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why semicolon #11

Closed
codeyash opened this issue Oct 9, 2014 · 11 comments
Closed

Why semicolon #11

codeyash opened this issue Oct 9, 2014 · 11 comments

Comments

@codeyash
Copy link

codeyash commented Oct 9, 2014

Python users see things clean and never use ; to end statements. This is obvious for many langs but it looks extra in python.

While using grammar I found it mandatory to put semicolon (;) at end of each line which is looks unnecessary to me.

I hope you got my point :) What is your opinion?

Thanks,
Yash , KineticWing IDE

@igordejanovic
Copy link
Member

There are two ways to define grammars in Arpeggio. A canonical is based on Python expressions and statements. The other is a textual PEG syntax and it is implemented using the Arpeggio itself (you can find the grammar of PEG language in the arpeggio.peg module). This implementation is based on PEG language and it requires that each rule terminates with ";" (see the rule function in the arpeggio.peg).

The PEG grammar definition is parsed using Arpeggio instantiated with the grammar from arpeggio.peg and during semantic analysis a new parser is constructed using your PEG definition. While a PEG should be more or less portable between different parsers if you want maximum performance use Python for grammar definition. Or, if you want to build a language give textX a try.

Best regards,
Igor

@codeyash
Copy link
Author

codeyash commented Oct 9, 2014

I'm not interested in python based functions as they will make it very specific to this lib.

Yes I agree for portability it should contain but it should be optional in lib itself. I mean if someone write it internally ignore that and make lib grammar reading easy.

<- can become =
Generic case ( Current version)
operation <- symbol operator (literal, functioncall);
expression <- literal operation functioncall;
expressionlist <- expression ("," expression)*;

Clean looking ( Requested version)
operation = symbol operator (literal, functioncall)
expression = literal operation functioncall
expressionlist = expression ("," expression)*

I guess I may be wrong but I found this style much everywhere so yes you can use it. Or provide some internal ways to support both. If it sounds interesting.

I want to parse PHP and similar web languages I'm not sure textX will be suited in that case or not. I don't want to parse full things only desired portion from PHP files and similar other langs. I'm not designing any lang just parsing few existing web langs.

Thanks,
Yash

@igordejanovic
Copy link
Member

I agree that this is cleaner looking. The current syntax is based on the original papers on PEG.

PEG language in arpeggio.peg module is just one of the languages implemented in Arpeggio. Consider it as an example of what can be done in Arpeggio and change it as you see fit.

The syntax is very easy to alter. Make a copy of module arpeggio.peg and remove ";" from the end of the rule and change "<-" to "=" in the LEFT_ARROW.
Use this altered module in place of the original arpeggio.peg.

Let me now if this works for you.

Best,
Igor

@codeyash
Copy link
Author

codeyash commented Oct 9, 2014

Cool . Thank you. Let me try it. 😄

@codeyash
Copy link
Author

codeyash commented Oct 9, 2014

I followed your instruction and created a cleanpeg module with above mention changes.

Unfortunately it didn't work
Error log
http://pastebin.com/Z7m6FND9

Used grammer

// Grammar is defined using textual specification based on PEG language.
calc_grammar = """
number = r'\d_.\d_|\d+'
factor = ("+" / "-")? (number / "(" expression ")")
term = factor (( "" / "/") factor)
expression = term (("+" / "-") term)*
calc = expression+ EOF
"""
Imp point from log..I think is
!! EOF not matched.

Please let me know if I'm doing anything wrong.

Thanks,
Yash

@igordejanovic
Copy link
Member

Don't have time to look more thoroughly but I think that the grammar is ambiguous now.
The lhs of the next rule is matched as the part of the previous rule. Your best bet is to leave rule termination as is (";") so that the parser will now when the rule ends. The "=" is ok.

Regards,
Igor

@igordejanovic
Copy link
Member

Actually, the PEG grammar can't be ambiguous and in this case the lhs will always be matched as a part of previous rule so the "=" will never be matched. Still, the best is to leave rule termination (";") or you could alter the grammar to use syntax predicates (e.g. Not) to check if the next part of the rule is not followed by the "=" and to match is only if it is not. This will ensure that lhs of the following rule is never matched by the previous rule.
Hope this helps,
Igor

@codeyash
Copy link
Author

codeyash commented Oct 9, 2014

ok with ; and <- it works perfectly. It looks we need to change little more about handling ending of rules.

I've a idea internally you can check for /n if that is possible instead of ;

Thanks,
Yash

@igordejanovic
Copy link
Member

Just added change on branch cleanpeg. Check this commit.

Regards,
Igor

@codeyash
Copy link
Author

codeyash commented Oct 9, 2014

Great 👍 😄 Let me try it

@codeyash
Copy link
Author

codeyash commented Oct 9, 2014

Work superbly no error reported this time. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants