473,750 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursion in grammar?

I'm trying to create Python parser/interpreter using ANTLR.
Reading grammar from language refference I found:
or_expr::= xor_expr | or_expr "|" xor_expr

For me it looks like infinite recursion. And so it says ANTLR. Maybe I
don't understand EBNF notation. For me it should look like this.
or_expr::= xor_expr | xor_expr "|" xor_expr

and in ANTLR grammar file like this:

or_expr: xor_expr { "\|" xor_expr }, or rather
or_expr: xor_expr ( "\|" xor_expr )*

Do I think good?
ANyone heard of Python grammar for ANTLR?
Jul 18 '05 #1
5 3421
pe*******@poczt a.onet.pl (Peri) writes:
I'm trying to create Python parser/interpreter using ANTLR.
Reading grammar from language refference I found:
or_expr::= xor_expr | or_expr "|" xor_expr

For me it looks like infinite recursion.
Isn't this just left recursion (a standard LL(1) trick)?
And so it says ANTLR. Maybe I don't understand EBNF notation. For
me it should look like this. or_expr::= xor_expr | xor_expr "|"
xor_expr


That wouldn't let you write "1 | 2 | 4", would it?

It's not really different from recursion in programming languages (or
mathematical induction) -- the Python grammar is a finite way of
describing sequences of tokens of arbitrary length. You have to have
*some* trick like recursion in there for this to work.

I can't help you with ANTLR.

Cheers,
mwh
--
I would hereby duly point you at the website for the current pedal
powered submarine world underwater speed record, except I've lost
the URL. -- Callas, cam.misc
Jul 18 '05 #2

"Peri" <pe*******@pocz ta.onet.pl> wrote in message
news:34******** *************** ***@posting.goo gle.com...
I'm trying to create Python parser/interpreter using ANTLR.
Reading grammar from language refference I found:
or_expr::= xor_expr | or_expr "|" xor_expr

For me it looks like infinite recursion.


Better to call it unbounded recursion. This is what makes general CF
languages/grammars more extensive/powerful that regular languages.

In particular, the recursion is written as left recursion.
The equivalent right recursion would be
or_expr::= xor_expr | xor_expr "|" or_expr
The recursive 'call' is to the right of the connector literal instead of
the left.

There are two types of linear CF parsers: bottom-up LR and top-down LL.
Each handles recursion written one way and chokes on recursion written the
other way. The two types have opposite requirements. (I forget which is
which.) Since Antler choked on left recursion, I presume that it is the
'other' type than Python's and that it requires right rather than left
recursion. So try reversing all the recursive definitions. (Or try first
the one that gives first error message and see if you get farther.)

Terry J. Reedy
Jul 18 '05 #3
On Sat, 27 Dec 2003 13:04:15 -0500, "Terry Reedy" <tj*****@udel.e du>
wrote:

"Peri" <pe*******@pocz ta.onet.pl> wrote in message
news:34******* *************** ****@posting.go ogle.com...
I'm trying to create Python parser/interpreter using ANTLR.
Reading grammar from language refference I found:
or_expr::= xor_expr | or_expr "|" xor_expr

For me it looks like infinite recursion.


Better to call it unbounded recursion. This is what makes general CF
languages/grammars more extensive/powerful that regular languages.

In particular, the recursion is written as left recursion.
The equivalent right recursion would be
or_expr::= xor_expr | xor_expr "|" or_expr
The recursive 'call' is to the right of the connector literal instead of
the left.

There are two types of linear CF parsers: bottom-up LR and top-down LL.
Each handles recursion written one way and chokes on recursion written the
other way. The two types have opposite requirements. (I forget which is
which.) Since Antler choked on left recursion, I presume that it is the
'other' type than Python's and that it requires right rather than left
recursion. So try reversing all the recursive definitions. (Or try first
the one that gives first error message and see if you get farther.)


Unless I am seriously mistaken, bottom-up LR parsers do not choke on
either left or right recursion. Right recursion, IIRC, requires
unbounded stack space in principle, but works OK in practice for the
simple reason that the input always defines a finite bound. It is
certainly less efficient, and carrys the risk that a complex input
might overflow the available stack space, but those are rarely serious
issues these days.

If LR parsing could not handle both left and right recursion, it
equally could not handle both left-associative and right-associative
operators. Of course most real parsers have specialised support to
optimise these common cases, but pure-form bottom-up LR parsing
algorithms such as LR(1) and LALR(1) are perfectly capable of handling
both left and right associative operators, and they do so using left
and right recursion as appropriate.

Which raises an important point - if you change the grammar to swap
the recursions from left to right or whatever, that results in a major
code-breaking change in how the parser interprets the language.
Left-associative expressions will be parsed as right-associative and
visa versa. Maybe worthwhile as a test to see what happens, but no
good for a final parser that is intended to work on real Python code.
Well, not without some post-processing of the resulting AST anyway.

ANTLR definitely uses LL parsing. I don't know about Pythons parsing
engine, though I suspect it uses LR-style parsing. It is a basic fact
of life that LL parsers and LR parsers do have quite different
limitations, and if Python uses LR parsing it will probably be
impossible to build a parser for the (unmodified) Python grammar using
an LL tool such as ANTLR. However, I find it hard to believe that
ANTLR has no means of working around these issues with some relatively
simple tweaks to the grammar specification.

I would raise the question in comp.compilers, or check if ANTLR has a
mailing list.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #4
Stephen Horne <st***@ninereed s.fsnet.co.uk> writes:
ANTLR definitely uses LL parsing. I don't know about Pythons parsing
engine, though I suspect it uses LR-style parsing.


Nope, LL(1). One thing you should note is that the grammar in the
docs is *not* the grammar used by Python's parser generator -- that's
Grammar/Grammar in the source distribution. I'm not sure, but I
suspect that the grammar in the docs is nastily ambiguous. Certainly
the actual Python parser lets through some stuff that get's thrown out
in the compiler with SyntaxErrors.

Cheers,
mwh

--
Good? Bad? Strap him into the IETF-approved witch-dunking
apparatus immediately! -- NTK now, 21/07/2000
Jul 18 '05 #5
Michael Hudson <mw*@python.net > wrote in message news:<m3******* *****@pc150.mat hs.bris.ac.uk>. ..
Stephen Horne <st***@ninereed s.fsnet.co.uk> writes:
ANTLR definitely uses LL parsing. I don't know about Pythons parsing
engine, though I suspect it uses LR-style parsing.


Nope, LL(1). One thing you should note is that the grammar in the
docs is *not* the grammar used by Python's parser generator -- that's
Grammar/Grammar in the source distribution. I'm not sure, but I
suspect that the grammar in the docs is nastily ambiguous. Certainly
the actual Python parser lets through some stuff that get's thrown out
in the compiler with SyntaxErrors.

Cheers,
mwh


Even the file Grammar/Grammar isn't quite LL(1). It's close, but not
quite. For example:

comp_op: '<'|'>'|'=='|'> ='|'<='|'<>'|'! ='|'in'|'not' 'in'|'is'|'is'
'not'

Would never be able to get to the 'is' 'not' section with LL. I
believe the parser generator straightens this out when it builds the
DFA's.
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1506
by: Bengt Richter | last post by:
We have where syntax in combination with suite expression syntax (bear with me, I think a good synergy will emerge ;-) http://groups.google.co.uk/groups?selm=mailman.403.1105274631.22381.python-list%40python.org http://groups.google.co.uk/groups?selm=3480qqF46jprlU1%40individual.net are the key referneces for background (I'm just repeating from Oren's post for convenience). The first suggests a grammar mod for its proposed "with"...
0
1693
by: Chad Whitacre | last post by:
Hey all, I've been playing around with the parser module, and based on the documentation I would expect all symbols in a parse tree to be part of the grammar. For example, I find this line in the symbol module docs: Refer to the file Grammar/Grammar in the Python distribution for the definitions of the names in the context of the language grammar. However, the program below gives me a human-readable parse tree (also
2
1773
by: Peter Rilling | last post by:
I am written a program that will be used to parse the lexical syntax of code files. I would like to generalize the grammar logic so that I don't hardcode any specific grammar in my program. Basically I would like to use XML to define a languages grammar the way that BNF does. Are there any standard XML format for defining a languages grammar?
10
2520
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... Thanks.
3
8143
by: junky_fellow | last post by:
I got one link to the ANSI C Grammar http://www.lysator.liu.se/c/ANSI-C-grammar-y.html However, I don't know how to understand this grammar. I am not a Computer Science Guy. Can anybody please help me about how to understand this grammar. Is there any good book on this ? Or anybody knows about some link from where a beginner like me start of ? Thanx for any help in advance ...
14
2039
by: Magius | last post by:
Hello, I have a question about the correctness of the language grammar for the C# 2.0 specification. I am working with the grammar specified in the June 2005 ECMA-334 standard. Basically, a using-namespace-directive is defined as follows: using-namespace-directive: using namespace-name ;
20
2997
by: athar.mirchi | last post by:
..plz define it.
8
1558
by: smartbeginner | last post by:
The Problem is CFG grammar generation Eg: Input S->ABC A->bcd B->efg C->hij It should produce the o/p bcdefghij substituting for every Capitals found on the way recursively The C pgm I wrote was:
35
4737
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
8999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9338
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.