Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can create a lexical error in C?

Besides not closing a comment /*..., what constitutes a lexical error in C?

like image 384
DrBeco Avatar asked Apr 04 '11 06:04

DrBeco


2 Answers

Here are some:

 "abc<EOF>

where EOF is the end of the file. In fact, EOF in the middle of many lexemes should produce errors:

 0x<EOF>

I assume that using bad escapes in strings is illegal:

  "ab\qcd"

Probably trouble with floating point exponents

 1e+%

Arguably, you shouldn't have stuff at the end of a preprocessor directive:

#if x   %
like image 196
Ira Baxter Avatar answered Oct 12 '22 09:10

Ira Baxter


Basically anything that is not conforming to ISO C 9899/1999, Annex A.1 "Lexical Grammar" is a lexical fault if the compiler does its lexical analysis according to this grammar. Here are some examples:

"abc<EOF> // invalid string literal (from Ira Baxter's answer) (ISO C 9899/1999 6.4.4.5)

'a<EOF> // invalid char literal (6.4.4.4)

where EOF is the end of the file.

double a = 1e*3; // misguided floating point literal (6.4.4.2)

int a = 0x0g; // invalid integer hex literal (6.4.4.1)

int a = 09; // invalid octal literal (6.4.4.1)

char a = 'aa'; // too long char literal (from Joel's answer, 6.4.4.4)

double a = 0x1p1q; // invalid hexadecimal floating point constant (6.4.4.2)
// instead of q, only a float suffix, that is 'f', 'l', 'F' or 'L' is allowed.

// invalid header name (6.4.7)
#include <<a.h>
#include ""a.h"
like image 39
Peter G. Avatar answered Oct 12 '22 09:10

Peter G.