473,802 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to in Python

I've got a pointer to a position in a line of code that contains
either a digit or a period (decimal point). I've got this comment:

Numbers are one of these:
integers:
digit+
0xhex_digit+
decimals:
digit+.digit*[E['+'|'-']digit+]
.digit+[E['+'|'-']digit+]
digit+[.digit*]%
.digit+%

Common metacode: '*' = 0 or more, '+' = 1 or more, [] = optional, | =
or, ...

Now I need to instantiate the comment. How would an experienced Python
coder proceed?
Dec 19 '07 #1
8 1180
En Wed, 19 Dec 2007 14:02:00 -0300, <Ma************ @gmail.comescri bi�:
I've got a pointer to a position in a line of code that contains
either a digit or a period (decimal point). I've got this comment:

Numbers are one of these:
integers:
digit+
0xhex_digit+
decimals:
digit+.digit*[E['+'|'-']digit+]
.digit+[E['+'|'-']digit+]
digit+[.digit*]%
.digit+%

Common metacode: '*' = 0 or more, '+' = 1 or more, [] = optional, | =
or, ...

Now I need to instantiate the comment. How would an experienced Python
coder proceed?
Do you have to validate input based on that grammar? That is, do you want
a function like this?

def is_valid_number (input):
if ....
return True
return False

Or, do you want to consume characters starting from your pointer, stopping
when an invalid character appears?

This looks like one of those few cases where "Use a regular expression"
may be a good answer. If you like a more Pythonic approach, try using
Pyparsing <http://pyparsing.wikis paces.com/>

--
Gabriel Genellina

Dec 19 '07 #2
On Dec 20, 4:02 am, MartinRineh...@ gmail.com wrote:
I've got a pointer to a position in a line of code that contains
either a digit or a period (decimal point). I've got this comment:

Numbers are one of these:
integers:
digit+
0xhex_digit+
decimals:
digit+.digit*[E['+'|'-']digit+]
.digit+[E['+'|'-']digit+]
digit+[.digit*]%
.digit+%

Common metacode: '*' = 0 or more, '+' = 1 or more, [] = optional, | =
or, ...

Now I need to instantiate the comment. How would an experienced Python
coder proceed?
Use a proper lexer written by somebody who knows what they are doing,
as has already been recommended to you.
Dec 19 '07 #3


John Machin wrote:
Use a proper lexer written by somebody who knows what they are doing,
as has already been recommended to you.
My lexer returns a MALFORMED_NUMBE R token on '0x' or '0x '. Try that
in Python.

Dec 21 '07 #4
If I get to add multi-line strings today, I'll have a complete
tokenizer. Interior looks a lot like C minus semi-colons. (Though I
did figure out that there wasn't any need for tokens that didn't come
from a real to have a doubleValue field. In C++ or Java all the Tokens
had a doubleValue, because one needed it.)

Theory: there are some problems which, by their nature, end up looking
a lot like C, regardless of language.

I wrote a Perl-to-HTML pretty-printer. It lets you embed HTML in
comments and creates a table of contents hyperlinked to the individual
functions. It's at http://www.MartinRinehart.com , output and source
code, in the Articles section.

I started with a Perl-style solution: regex for the chars left of "#"
and for the chars to the right. One line of Perl. Nice, except that it
won't work when applied to itself. It will split the line based on the
"#" in the regex, of course. Kludged around that, but then met "#"
embedded in strings, "#" embedded in regex passed to functions, ...

Ended up marching down the input line, one character at a time, like a
C program.
Dec 21 '07 #5
On Dec 21, 2007 7:25 AM, <Ma************ @gmail.comwrote :
>

John Machin wrote:
Use a proper lexer written by somebody who knows what they are doing,
as has already been recommended to you.

My lexer returns a MALFORMED_NUMBE R token on '0x' or '0x '. Try that
in Python.
Is there some reason that you think Python is incapable of
implementing lexers that do this, just because Python lexer accepts
it? Note that if you're using your lexer to mark up or pretty print or
whatever Python source, it's wrong - 0x is (rightly or not) a valid
Python literal.
Dec 21 '07 #6


Chris Mellon wrote:
Is there some reason that you think Python is incapable of
implementing lexers that do this, just because Python lexer accepts
it?
Absolutely not. My opinion is that it's a bug. A very, very minor bug,
but still six-legged.
Note that if you're using your lexer to mark up or pretty print or
whatever Python source, it's wrong - 0x is (rightly or not) a valid
Python literal.
My lexer is for my language, Decaf, which, in this particular, is the
same as Python. Here's what I find at at python.org/ref: (2.4.4).

hexinteger ::= "0" ("x" | "X") hexdigit+

Implementation differs from specification. In this case, I think the
spec is more sensible.
Dec 21 '07 #7
On Dec 21, 2007 1:02 PM, <Ma************ @gmail.comwrote :
>

Chris Mellon wrote:
Is there some reason that you think Python is incapable of
implementing lexers that do this, just because Python lexer accepts
it?

Absolutely not. My opinion is that it's a bug. A very, very minor bug,
but still six-legged.
I understand the argument but I'm wondering why you're asking
questions on this list about it. You don't seem to be implementing the
lexer in Python (because otherwise the question wouldn't have come
up), and you don't seem to be parsing Python code. Where's the python
angle? Not that this list is totally intolerant of offtopic
discussions, but you don't seem to even have a hint of Python interest
here - the questions seem to be better suited for comp.parsers.ge neral
or something.
Note that if you're using your lexer to mark up or pretty print or
whatever Python source, it's wrong - 0x is (rightly or not) a valid
Python literal.

My lexer is for my language, Decaf, which, in this particular, is the
same as Python. Here's what I find at at python.org/ref: (2.4.4).

hexinteger ::= "0" ("x" | "X") hexdigit+

Implementation differs from specification. In this case, I think the
spec is more sensible.
I tend to consider "what the parser actually accepts" rather than
"what the grammar specifies" to be normative when writing pretty
printers or syntax highlighters.
Dec 21 '07 #8


Chris Mellon wrote:
You don't seem to be implementing the
lexer in Python
I am absolutely implementing my language in Python, a language I have
now been writing for two entire weeks. This list has been more than
helpful, tolerating numerous newbie questions.
Dec 22 '07 #9

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

Similar topics

0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10304
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...
0
10063
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
9114
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...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6838
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
5494
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...
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
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.