8/23/2007

S-expression

I wonder what the definition of an S-expression is. Here is the most naive description I reached.
expr: TERM
| '(' expr ')'
| expr expr
;
where the TERM means a sequence of alphabets and/or numbers (and/or symbols except ')' and '(', though I omit these so far).

The problem of the above definition is that that accept two sequential S-expressions "(aa) (bb)" as an S-expression. It's curious. I'm becoming aware of that such a syntax rule might not define the S-expression. This means I must write the real lexical analyzer.

Now I use this infantile code with the YACC.
yylex()
{
  char c;
  while ((c=getchar())==' ' || c=='\t')
    ;
  if (c == EOF)
    return 0;
  if (isalnum(c)){
    char sbuf[100], *p = sbuf;
    do {
      *p++ = c;
      } while ((c=getchar()) != EOF && isalnum(c));
    ungetc(c, stdin);
    *p = '\0';
    printf("%s ", sbuf);
    return TERM;
  }
  if (c == '\n'){
    lineno++;
    return '\n';
  }
  return c;
}

0 Comments:

Post a Comment

<< Home