OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Jun 16, 2019 1:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Progress of my C compiler
PostPosted: Thu Feb 14, 2019 7:52 pm 
Offline
Member
Member
User avatar

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 633
It figures out that this:

Code:
void (*signal(int sig, void (*func)(int)))(int);


Means this (converts to internal representation, then prints it in human-readable form):

Code:
extern `signal' as function taking (signed int, pointer to function taking (signed int) returning void) returning pointer to function taking (signed int) returning void

_________________
Glidix: An x86_64 POSIX-compliant operating system, aiming to be as optimized as possible, especially in graphics.
https://glidix.madd-games.org/


Top
 Profile  
 
 Post subject: Re: Progress of my C compiler
PostPosted: Thu Feb 28, 2019 4:20 am 
Offline
Member
Member
User avatar

Joined: Mon Mar 05, 2012 11:23 am
Posts: 580
Location: Germany
Nice :mrgreen:

Parsing function pointer syntax must be a real pita. Which approach did you use, LR-parser with a lookahead?

_________________
Ghost OS - GitHub


Top
 Profile  
 
 Post subject: Re: Progress of my C compiler
PostPosted: Sun Mar 17, 2019 7:29 am 
Offline
Member
Member
User avatar

Joined: Sat Oct 16, 2010 3:38 pm
Posts: 633
max wrote:
Nice :mrgreen:

Parsing function pointer syntax must be a real pita. Which approach did you use, LR-parser with a lookahead?


I used a recursive descent parser (https://gitlab.com/madd-games/comptools ... cc-parse.c).

Parsing the function pointer syntax becomes simple once you build a parse tree according to the grammar in the C standard. Each node essentially applies more modifiers to the base type. In some cases, such as arrays, you have to traverse the nodes right-to-left, but that too is doable. Most of the hard work happens here: https://gitlab.com/madd-games/comptools ... ast.c#L363

The simple way of looking at it is this: look at the declarator (the variable/function name) and apply modifiers moving outwards, right-to-left.

Code:
int (*x)[5];


x -> it's called "x"
(*x) -> it's a pointer
[5] -> it's an array of 5
int -> ints

as opposed to:

Code:
int *x[5]


x -> it's called "x"
[5] -> it's an array of 5
* -> pointers
int -> to int

_________________
Glidix: An x86_64 POSIX-compliant operating system, aiming to be as optimized as possible, especially in graphics.
https://glidix.madd-games.org/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group