C grammar

aeb at turing.UUCP aeb at turing.UUCP
Wed Feb 13 06:43:50 AEST 1985


Having the personality of a kumquat and wanting to befuddle my friends
I cranked some of my C programs through the recently posted parser.
Unfortunately it complained about correct fragments like

    ...
    if ((*buf == ':') || (('a' <= *buf) && ('z' >= *buf)))
    ...

because it parsed all of ':') || ..... ('z' as one single character constant.
Also the handling of backslashes in strings was not quite correct.

The diff below shows how one might change scan.l in order to improve
the parser's behaviour. I am not sure about the + . Are multi-character
character constants permitted by the new standard? (I hope not.)

*** scan.l.orig	Tue Feb 12 11:38:07 1985
--- scan.l	Tue Feb 12 17:16:09 1985
***************
*** 56,62
  0{D}+{US}?{LS}?		{ count(); return(CONSTANT); }
  {D}+{LS}?{US}?		{ count(); return(CONSTANT); }
  {D}+{US}?{LS}?		{ count(); return(CONSTANT); }
! '.*'			{ count(); return(CONSTANT); }
  
  {D}+{E}{LS}?		{ count(); return(CONSTANT); }
  {D}*"."{D}+({E})?{LS}?	{ count(); return(CONSTANT); }

--- 56,62 -----
  0{D}+{US}?{LS}?		{ count(); return(CONSTANT); }
  {D}+{LS}?{US}?		{ count(); return(CONSTANT); }
  {D}+{US}?{LS}?		{ count(); return(CONSTANT); }
! '(\\.|[^\\'])+'		{ count(); return(CONSTANT); }
  
  {D}+{E}{LS}?		{ count(); return(CONSTANT); }
  {D}*"."{D}+({E})?{LS}?	{ count(); return(CONSTANT); }
***************
*** 62,68
  {D}*"."{D}+({E})?{LS}?	{ count(); return(CONSTANT); }
  {D}+"."{D}*({E})?{LS}?	{ count(); return(CONSTANT); }
  
! \"(\\\"|[^"])*\"	{ count(); return(STRING_LITERAL); }
  
  ">>="			{ count(); return(RIGHT_ASSIGN); }
  "<<="			{ count(); return(LEFT_ASSIGN); }

--- 62,68 -----
  {D}*"."{D}+({E})?{LS}?	{ count(); return(CONSTANT); }
  {D}+"."{D}*({E})?{LS}?	{ count(); return(CONSTANT); }
  
! \"(\\.|[^\\"])*\"	{ count(); return(STRING_LITERAL); }
  
  ">>="			{ count(); return(RIGHT_ASSIGN); }
  "<<="			{ count(); return(LEFT_ASSIGN); }
-- 
      Andries Brouwer -- CWI, Amsterdam -- {philabs,decvax}!mcvax!aeb



More information about the Comp.lang.c mailing list