need help on this question:

Write a lexer to read in decimal numbers. If your input is
the following string: “12345.9876” what do you have to do to each character in the string
to generate your float? How are the numbers before the decimal point different to the
numbers after the decimal point? and draw a state transition diagram for the scanning part
of your lexer.

is this coding right:

input = raw_input () # Grab raw keyboard input
4 int_string = "12345.9876"
5
6 for c in input :
7 if c. isdigit (): # state : digit
8 int_string = int_string + c
9 else : # state : accept
10 break

Recommended Answers

All 8 Replies

Please use CODE tags! It's pretty much essential for Python so I can tell if your indentation is off. That being said, I put your code in tags for you:

myinput = raw_input() # Grab raw keyboard input
int_string = "12345.9876"

for c in myinput :
    if c.isdigit (): # state : digit
        int_string += c
    else: # state : accept
        break

Refrain from using 'input' as a variable name as it's already the name of a Python function; I changed 'input' to 'myinput'.

What exactly is it you need to do to? Sorry, I'm just a little confused with your description about state-transition diagrams and such. Why are you appending the digit characters from the user input into the int_string?
I'm just not fully sure what it is you're wanting to accomplish. The above code should work fine, but to what end?

this is the string you have to write in a lexer to read in decimal numbers “12345.9876”

And? Sorry if it seems obvious, but can you at least put a bit of depth into that response?
Running your script yields something like:

376.87  #what I input.
12345.9876376  #int_string afterwards

So what exactly do you mean by 'a lexer to read decimal numbers'? As in floats (numbers with a decimal point), or as in numbers with a base of 10 (as opposed to hex or binary numbers)?

yes as in a float ive just check the question which i showed it missing a line this was the question:

Write a lexer to read in decimal numbers. Think hard about how to do this. If your input is
the following string: “12345.9876” what do you have to do to each character in the string
to generate your float? How are the numbers before the decimal point different to the
numbers after the decimal point? Make sure you have an algorithm that works correctly
on paper before you start coding and draw a state transition diagram for the scanning part
of your lexer.

can shead so light on the question on the above post

So if I read that correctly, you need a way of turning "12345.9876" into a float without using the float() function, but by making your own?

you have to generate a float so i think you can use the float() function

can some plz show me or give me an example of how to write this lexer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.