Thursday, September 12, 2013

Syntax Analysis Example 2

dfs (G)
    clock = 1
    for each v in V
        visited[v] = false
    for each v in V
        if !visited[v]
            explore(G, v)

explore (G, v)
    visited[v] = true
    previsit(v)
    for each (v,u) in E
        if !visited[u]
            explore(u)
    postvisit(G, v)

previsit(v)
    pre[v] = clock++

postvisit(v)
    post[v] = clock++

This example is much for "codey" than the previous example (i.e. it looks more like real code). One thing I noticed in this example that I don't remember seeing in the other is inconsistency of spacing between function names and parenthesis. I guess I could allow 0 or 1 space, but I'm not sure how other languages handle that.

We have a couple new concepts in this example -- primarily "for each" syntax and array access. Python has a "for each" loop, but without the "each". So converting this to Python would be relatively simple; just drop the "each" and add a ":" to the end. Python also does array access with the square bracket notation, so that doesn't need changing.

However, Python strictly uses word representations for logical operators, instead of symbols (e.g. ! vs not). I would want the user to be able to use either (I usually remember to use "and" and "or" when I switch back to Python from a more strict language, but I sometimes forget to use "not" instead of the bang).

Python has augmented assignment operators, but it doesn't allow for increment or decrement operators. That is you can write "x += 1", but you can't write "x++". I like the increment/decrement operators, though. They're simple and quick. I think I might simply my life by just having them be postfix, though. No prefix allowed! (but I'm sure there would be some users that would want prefix... hmm)

CONCLUSION:
  • There should be 0 or 1 space between a keyword and its parenthesis.
  • The word "each" is allowed, but ignored in a "for each" loop. 
  • Consequently, the word "foreach" is allowed, but will be treated as "for each".
  • Array access should be done with square bracket notation.
  • Logical operators can be represented by their English label or symbols (e.g. "and" vs "&&" or "not" vs "!").
  • Increment and decrement operators are allowed, but will only be treated as postfix.
  • Augmented assignment operators are allowed.

No comments:

Post a Comment