473,612 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parser of parentheses in arithmetical expressions

1 New Member
from pyparsing import *
cislo = Word(nums)
levaZavorka = Suppress('(')
pravaZavorka = Suppress(')')

vyraz = Forward()
vyraz << Or( [cislo,
Group(levaZavor ka + vyraz + "+" + vyraz + pravaZavorka),
Group(levaZavor ka + vyraz + "-" + vyraz + pravaZavorka),
Group(levaZavor ka + vyraz + "*" + vyraz + pravaZavorka)] )

celyVyraz = vyraz + StringEnd()

so we need to change vyraz

it must work like that

print(celyVyraz .parseString('3 '))
print(celyVyraz .parseString('( 3+5)'))
print(celyVyraz .parseString('( (3+5)-2)'))
print(celyVyraz .parseString('( (3+5)-(2*4))'))
print(celyVyraz .parseString('( (3+5)+4))'))
print(celyVyraz .parseString('( ((3+5)-4)-2))'))
print(celyVyraz .parseString('3 +5-2*4'))

Thank you!
Mar 31 '17 #1
0 1114

Sign in to post your reply or Sign up for a free account.

Similar topics

72
4385
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the python-dev list: http://www.python.org/peps/pep-0289.html In brief, the PEP proposes a list comprehension style syntax for creating fast, memory efficient generator expressions on the fly: sum(x*x for x in roots)
1
4162
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
6
1517
by: Norm Dotti | last post by:
In VS 2002 I could specify Dim x As System.Windows.Forms.Form() or Dim x As System.Windows.Forms.Form and everything was ok. Since upgrading to VS 2003 it won't let me put in the parentheses in the above sample. After I
0
1490
by: Phlip | last post by:
C++ newsgroupies: I wrote a parser to solve math expressions like "3.0 ^(4 - 5)", or "3 / 8". Below my sig is recursiveDescentParser.cpp, the test suite that drove the implementation of the expression solver found in recursiveDescentParser.h, in a parallel post. Test-Driven Development works by accumulating assertions. Before the very first assertion existed...
6
9004
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I've started using regular expressions for parsing some data string that come along - works quite nicely, however, as a newbie to RE, I'm still struggling with some special cases: 1) I get strings representing decimal values, and they have the following format: * first, a plus or minus sign * then, a number of numeric chars
7
1985
by: Joshua Morgan | last post by:
Hey all, I want to create my own XML parser that is completely independent of PHP version (i want it to run on both PHP 4 and 5), and to also not require any dependencies (so that the user DOESN'T have to compile php with --with-xml). Can someone point me in the right direction as to how I can go about this?
2
3793
by: André Hänsel | last post by:
Hi, before I rack my brain about something others have thought about, here my question: Is there any description how to write a parser that evaluates expressions with logical operators like "(condition1 OR condition2 AND (condition3 OR condition4))" in the right way? Best regards,
20
3398
by: Geoff Hill | last post by:
What's the way to go about learning Python's regular expressions? I feel like such an idiot - being so strong in a programming language but knowing nothing about RE.
13
7472
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can be accomplished with regular expressions this way, such as validating a mathematical expression or parsing a language with nested parens, quoting or expressions. Another feature I'm missing is once-only subpatterns and possessive quantifiers...
0
2200
by: jpecci | last post by:
I am approaching parsing for the first time and I can't find which solution is best for me. I need to process command line strings implementing mathematical operations (defined by me) on objects (defined by me), i.e. Prompt:> ((A*B)->C)+2 A,B,C could be matrixes or more complex objects *, -> ,+ could be operations which I want to implement Is there a parser approach most suitable for this?
0
8162
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
8605
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...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8246
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
8415
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
7039
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
5532
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
4045
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.