LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2023, 06:09 AM   #1
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Rep: Reputation: 4
Where to post lexer file related issues.


Have a compiler book, that has incremental compiler construction, with source code at: https://highered.mheducation.com/sit...ce_codes_.html

In Chapter 2 source code, there is file: c-lex.l
Code:
D			[0-9]
L			[a-zA-Z_]
H			[a-fA-F0-9]
E			[Ee][+-]?{D}+
FS			(f|F|l|L)
IS			(u|U|l|L)*

%{


enum
{
  IDENTIFIER = 1, CONSTANT, STRING_LITERAL, SIZEOF, PTR_OP, 
  INC_OP, DEC_OP, LEFT_OP, RIGHT_OP, LE_OP, GE_OP, EQ_OP, NE_OP, 
  AND_OP, OR_OP,MUL_ASSIGN, DIV_ASSIGN, MOD_ASSIGN, ADD_ASSIGN, 
  SUB_ASSIGN, LEFT_ASSIGN,RIGHT_ASSIGN, AND_ASSIGN, XOR_ASSIGN, 
  OR_ASSIGN, TYPE_NAME, TYPEDEF,EXTERN, STATIC, AUTO, REGISTER, 
  CHAR, SHORT, INT, LONG, SIGNED, UNSIGNED,FLOAT, DOUBLE, CONST, 
  VOLATILE, VOID, STRUCT, UNION, ENUM, ELLIPSIS,CASE, DEFAULT, 
  IF, ELSE, SWITCH, WHILE, DO, FOR, GOTO, CONTINUE, BREAK,RETURN, 
  DEFINE, SEMI, LC, RC, COMMA, COLON, EQUAL, LP, RP, LB, RB,
  STRUCTOP, AND, NOT, NOR, MINUS, PLUS, STAR, DIVOP, MOD, LT, 
  GT, XOR, OR, QUEST, POUND, 
};
void comment();
#define	preturn(val)  \
   printf ("Lexeme=[%s] \t Length=%d \t Token is %s\n", \
           yytext, yyleng, #val ); return(val);

%}

%%
"/*"			{ comment(); }

%{
	/* Keywords */
%}

"auto"			{  preturn(AUTO); }
"break"			{  preturn(BREAK); }
"case"			{  preturn(CASE); }
"char"			{  preturn(CHAR); }
"const"			{  preturn(CONST); }
"continue"		{  preturn(CONTINUE); }
"default"		{  preturn(DEFAULT); }
"define"		{  preturn(DEFINE); }
"do"			{  preturn(DO); }
"double"		{  preturn(DOUBLE); }
"else"			{  preturn(ELSE); }
"enum"			{  preturn(ENUM); }
"extern"		{  preturn(EXTERN); }
"float"			{  preturn(FLOAT); }
"for"			{  preturn(FOR); }
"goto"			{  preturn(GOTO); }
"if"			{  preturn(IF); }
"int"			{  preturn(INT); }
"long"			{  preturn(LONG); }
"register"		{  preturn(REGISTER); }
"return"		{  preturn(RETURN); }
"short"			{  preturn(SHORT); }
"signed"		{  preturn(SIGNED); }
"sizeof"		{  preturn(SIZEOF); }
"static"		{  preturn(STATIC); }
"struct"		{  preturn(STRUCT); }
"switch"		{  preturn(SWITCH); }
"typedef"		{  preturn(TYPEDEF); }
"union"			{  preturn(UNION); }
"unsigned"		{  preturn(UNSIGNED); }
"void"			{  preturn(VOID); }
"volatile"		{  preturn(VOLATILE); }
"while"			{  preturn(WHILE); }

%{
	/* Identifier */
%}

{L}({L}|{D})*		{  preturn(IDENTIFIER); }

%{
	/* Constants */
%}

0[xX]{H}+{IS}?		{ preturn(CONSTANT); }
0{D}+{IS}?		{ preturn(CONSTANT); }
{D}+{IS}?		{ preturn(CONSTANT); }
L?'(\\.|[^\\'])+'	{ preturn(CONSTANT); }
{D}+{E}{FS}?		{ preturn(CONSTANT); }
{D}*"."{D}+({E})?{FS}?	{ preturn(CONSTANT); }
{D}+"."{D}*({E})?{FS}?	{ preturn(CONSTANT); }

%{
	/* String Literal */
%}

L?\"(\\.|[^\\"])*\"	{  preturn(STRING_LITERAL); }

%{
	/* Operators */
%}

"..."			{  preturn(ELLIPSIS); }
">>="			{  preturn(RIGHT_ASSIGN); }
"<<="			{  preturn(LEFT_ASSIGN); }
"+="			{  preturn(ADD_ASSIGN); }
"-="			{  preturn(SUB_ASSIGN); }
"*="			{  preturn(MUL_ASSIGN); }
"/="			{  preturn(DIV_ASSIGN); }
"%="			{  preturn(MOD_ASSIGN); }
"&="			{  preturn(AND_ASSIGN); }
"^="			{  preturn(XOR_ASSIGN); }
"|="			{  preturn(OR_ASSIGN); }
">>"			{  preturn(RIGHT_OP); }
"<<"			{  preturn(LEFT_OP); }
"++"			{  preturn(INC_OP); }
"--"			{  preturn(DEC_OP); }
"->"			{  preturn(PTR_OP); }
"&&"			{  preturn(AND_OP); }
"||"			{  preturn(OR_OP); }
"<="			{  preturn(LE_OP); }
">="			{  preturn(GE_OP); }
"=="			{  preturn(EQ_OP); }
"!="			{  preturn(NE_OP); }
";"			{  preturn(SEMI); }
("{"|"<%")		{  preturn(LC); }
("}"|"%>")		{  preturn(RC); }
","			{  preturn(COMMA); }
":"			{  preturn(COLON); }
"="			{  preturn(EQUAL); }
"("			{  preturn(LP); }
")"			{  preturn(RP); }
("["|"<:")		{  preturn(LB); }
("]"|":>")		{  preturn(RB); }
"."			{  preturn(STRUCTOP); }
"&"			{  preturn(AND); }
"!"			{  preturn(NOT); }
"~"			{  preturn(NOR); }
"-"			{  preturn(MINUS); }
"+"			{  preturn(PLUS); }
"*"			{  preturn(STAR); }
"/"			{  preturn(DIVOP); }
"%"			{  preturn(MOD); }
"<"			{  preturn(LT); }
">"			{  preturn(GT); }
"^"			{  preturn(XOR); }
"|"			{  preturn(OR); }
"?"			{  preturn(QUEST); }
"#"			{  preturn(POUND); }

[ \t\v\n\f]		{  }
.			{ /* ignore bad characters */ }

%%


void comment()
{
	int c=0, prev=0;

	/* eat up all characters till end of comment */
loop:
	prev= c;
	c = input();
	if( (prev == '*') && ( c== '/' )){
		return;
	}
	if( c == -1 ){
		exit(1);
	}
	goto loop;
}

int main(int argc, char **argv)
{
	FILE *fp;
	int ret_val;

	if(argc != 2){
		printf("Usage %s <input File>\n",argv[0]);
		exit(1);
	}

	if((fp = fopen(argv[1],"r")) == NULL ){
		printf("File [%s] does not exist \n",argv[1]);
		exit(1);
	}

	yyin = fp;	/* Input file to Lexical Analyzer */

	while (1) {
		if( (ret_val=yylex()) == 0 ){
			break;
		}
		/* return value is token type like BREAK, IDENTIFIER*/	
	}
}
Have issues that the below define for preturn() is having : \, stated in the definition, which is confusing to me, as what is its purpose.

Code:
#define	preturn(val)  \
   printf ("Lexeme=[%s] \t Length=%d \t Token is %s\n", \
           yytext, yyleng, #val ); return(val);
 
Old 07-03-2023, 06:13 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
https://stackoverflow.com/questions/...he-end-of-line
Quote:
A continued line is a line which ends with a backslash, \ . The backslash is removed and the following line is joined with the current one. No space is inserted, so you may split a line anywhere, even in the middle of a word. (It is generally more readable to split lines only at white space.)
 
1 members found this post helpful.
Old 07-03-2023, 01:41 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
To expand on what pan64 posted, in C preprocessor syntax a #define statement includes everything up to the first newline character. The \ hides the newline character which lets the definition extend across multiple lines in the source file.

It is important that the \ be the very last character on the line. A common error that can be difficult to see is having a space follow the \ so that the newline is no longer hidden.
 
1 members found this post helpful.
Old 07-03-2023, 07:16 PM   #4
ajiten
Member
 
Registered: Jun 2023
Posts: 375

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by astrogeek View Post
To expand on what pan64 posted, in C preprocessor syntax a #define statement includes everything up to the first newline character. The \ hides the newline character which lets the definition extend across multiple lines in the source file.

It is important that the \ be the very last character on the line. A common error that can be difficult to see is having a space follow the \ so that the newline is no longer hidden.
Does this line extension trick work in normal C code, or is used only in any macro (C) preprocessor?
If only in macro (C) preprocessor, then the next question IS: how is any macro preprocessor; or specifically, the C macro preprocessor coded?

Any source book, or site for the same, is requested.

Last edited by ajiten; 07-03-2023 at 07:37 PM.
 
Old 07-04-2023, 12:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,863

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
Quote:
Originally Posted by ajiten View Post
Does this line extension trick work in normal C code, or is used only in any macro (C) preprocessor?
What do you mean by normal c code, what is the non-normal c code?
the preprocessing phase is completely automatic, when you start to compile your code it will be first preprocessed (by the tool named cpp).
Quote:
Originally Posted by ajiten View Post
If only in macro (C) preprocessor, then the next question IS: how is any macro preprocessor; or specifically, the C macro preprocessor coded?

Any source book, or site for the same, is requested.
Using \ is quite general in unix/linux, you can use it in shell(s) and other languages too.
https://gcc.gnu.org/git.html
This is where you find the source code of gcc (inlcuding its own cpp).
And obviously here is the documentation: https://gcc.gnu.org/onlinedocs/gcc-13.1.0/cpp/

Last edited by pan64; 07-04-2023 at 12:49 AM.
 
Old 07-04-2023, 02:06 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,267
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
Quote:
Originally Posted by ajiten View Post
Does this line extension trick work in normal C code, or is used only in any macro (C) preprocessor?
As pan64 has already pointed out, suppression of newlines by a \ is a fairly common practice in the Unix/Linux world.

Whitespace, including newlines is not significant in the C language so in general there is no need to suppress them.

But, preprocessing directives, by definition, are lines bounded by a # character as the first non-whitespace character and terminated by a newline character. So, to format those across multiple lines for readability you must suppress all but the final bounding newline.

Quote:
Originally Posted by ajiten View Post
If only in macro (C) preprocessor, then the next question IS: how is any macro preprocessor; or specifically, the C macro preprocessor coded?

Any source book, or site for the same, is requested.
I would recommend K&R The C Programming Language, Second Edition (ANSI edition) which has a very complete section about the C preprocessor.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DNS issues, Downloading issues, Web issues. UbuntuHelp Linux - Networking 1 08-28-2012 07:34 AM
usb wireless networking issues possibly related to ehci_hcd module! GrayGh0st Fedora 4 12-29-2004 11:07 AM
nvidia + Xfree + kernel issues, probably HW accel related p-static Linux - Software 1 09-12-2004 07:46 PM
Kernel / Grub and Mozilla updates related issues taureanz Linux - General 0 08-27-2003 12:30 AM
[NOT REALLY LINUX RELATED] How do i fix loopback issues? vbp6us Linux - General 3 04-20-2003 01:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration