1

This question is asked before.

I have already been to these threads - Thread 1, Thread 2

I have just started reading JavaScript the Good Parts and it consists of railroad diagrams which i can't understand even after visiting Wikipedia. You can't just skip them, there are lot's of them. I don't even know why they are so confusing, i found JavaScript to be easy.

Let's take these diagrams for example.

Fraction Railroad Diagram

Exponent Railroad Diagram

Correct me if i'm wrong, or just tell me if i am studying these diagrams the right way, below.


For the first diagram, you start from the left, you encounter the "decimal point", Now there are two options. If we just go straight ahead, does that mean only a decimal point without any digit ahead is considered a fraction or is it assumed that there will be a zero ahead by javascript?

On the Second Route, we encounter decimal point and then we add a digit to it and then we have a choice either to add more digits in a loop or go ahead and finish with the digit.


For the second diagram, I am lost.

3
  • If after the decimal point, there are zeros only (for example 3. Or 3.00), js will store your number as integer, because storing integers consumes less memory than storing floating point numbers
    – Trash Can
    Aug 7, 2015 at 16:46
  • As a complement to the regular answers, if you're interested, railroad diagrams are a graphical representation of formal grammars. en.wikipedia.org/wiki/Syntax_diagram Aug 7, 2015 at 16:59
  • 1
    I love that book, but I personally find the railroad diagrams pretty uninteresting. It's nice that they're there for reference, but the Javascript syntax is straightforward enough that the book's examples will generally convey what you need to know. Just my 2 cents, but I'd recommend simply glancing over the diagrams and not getting too hung up on them.
    – Ghazgkull
    Aug 7, 2015 at 17:06

2 Answers 2

1

The first diagram means that a fraction is a sequence starting with a dot and followed by zero or more (possibly infinite) digits.

The second diagram means that an exponent starts off with either "e" or "E" then has an optional sign ("-"/"+") and then one or more digits.

In general those vertical segments with forks show various options that can be taken, while the loop represents that they can be run through zero or multiple times.

0
0

I had to pull my copy of the book off the shelf, but now I get it. The railroad diagram for 'fraction' is part of the larger railroad diagram for 'number literal' which looks like: integer[fraction][exponent]. Taken in that context, it's correct... you can have just a dot after the integer part of a number as in var num = 1.;

In the second diagram, branches represent valid choices. So you start with e or E. Then have a +, -, or nothing (which is the same as +). And finally a sequence of zero or more digits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.