HiveBrain v1.2.0
Get Started
← Back to all entries
patterncModerate

Lexical analyzer for a programming language

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lexicalprogramminglanguageforanalyzer

Problem

I'm doing a lexical analyzer for my programming language and I don't know if I'm doing it right. Can anyone can help me with this or suggest a better way of doing it?

```
#include
#include
#include
#include
#include

struct node
{
char character;
struct node *link;
}front = NULL, rear = NULL;

int isEmpty()
{
if(front == NULL)
return 1;
else
return 0;
}

char name[4] = "\0";

void enqueue(char character)
{
struct node *tmp;
tmp = (struct node *)malloc(sizeof(struct node));
if(tmp == NULL)
return;

tmp -> character = character;
tmp -> link = NULL;
if(front == NULL)
front = tmp;
else
rear -> link = tmp;
rear = tmp;
}

char dequeue()
{
struct node *tmp;
char character;
if(isEmpty())
return NULL;

tmp = front;
character = tmp -> character;
front = front -> link;
free(tmp);
return character;
}

char peek()
{
if(isEmpty())
return NULL;
return front -> character;
}

void display()
{
struct node *ptr;
ptr = front;
if(isEmpty())
return;

while(ptr != NULL)
{
printf("%c ", ptr -> character);
ptr = ptr -> link;
}
}

int issymbol(char c)
{
char symbols[] = {"!@#$%^&*()_-.?<>,+=-/\\"};

for(int b = 0; b < strlen(symbols); b++)
if(c == symbols[b])
return 0;
return 1;
}

int checkIdent(char ident)
{
if(issymbol(ident))
return 28;
else
return -2;
}

int automaton()
{
int i = 0;
char ident[50] = "\0";
char ch = '\0';
int next = 0;

while(1)
{
if(peek() == 'c') {
ident[i++] = dequeue();
if(peek() == 'a') {

Solution

Things you could improve:

Compilation:

-
I am led to believe that you are compiling your code as C++ code.

tmp = (struct node *)malloc(sizeof(struct node));


That line right there would be unacceptable when compiling as C code. And considering that your question originally had c++ on it, I am further convinced that you are compiling your C code as C++ code. Don't do that.

Here is a very long list of the incompatibilities between ISO C and ISO C++. Those are the reasons you compile C code, as C code.

If I happen to be wrong in my assumption that you are compiling C code as C++ code, you still should not be casting the results of malloc().

Variables/Initialization:

-
You shouldn't use global variables.

*front = NULL, *rear = NULL;


The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables.

To understand how the application works, you pretty much have to take into account every function which modifies the global state. That can be done, but as the application grows it will get harder to the point of being virtually impossible (or at least a complete waste of time).

If you don't rely on global variables, you can pass state around between different functions as needed. That way you stand a much better chance of understanding what each function does, as you don't need to take the global state into account.

So instead of using global variables, initialize the variables in main(), and pass them as arguments to functions if necessary.

-
You aren't initializing your arrays properly.

char name[4] = "\0";


You should be initializing all of your characters in the array at once.

char name[4] = {0};


Efficiency:

-
You can use the inline keyword on some of your functions. The point of making a function inline is to hint to the compiler that it is worth making some form of extra effort to call the function faster than it would otherwise - generally by substituting the code of the function into its caller. As well as eliminating the need for a call and return sequence, it might allow the compiler to perform certain optimizations between the bodies of both functions.

inline int isEmpty(Node front)


Returns:

-
You aren't returning a value from main().

void main()


You should always return a value from main(). This is an indication of the status of your program, and whether or not it exited successfully. This is very important information when it comes to debugging.

int main(void)
{
    ...
    return 0;  // standard return code for successive exit
}


-
NULL isn't a char, so you shouldn't treat it as one when you return it.

char peek()
{
 if(isEmpty())
     return NULL;
 return front -> character;
}


return the NUL-terminator character instead.

return '\0';


Syntax

-
typedef your structs, and declare and initialize them elsewhere.

struct node
{
    char character;
        struct node *link;
}*front = NULL, *rear = NULL;


The typedef means you no longer have to write struct all over the place. That not only saves some space, it also can make the code cleaner since it provides a bit more abstraction.

typedef struct
{
    char character;
    struct node *link;
} Node;


-
You can simplify your NULL checks.

if(front == NULL)


Since NULL is defined as (void *)0, we can treat is as a comparison to 0.

if (!front)


-
Use ternary operators when you have simple if-else statements.

if(front == NULL)
     return 1;
 else
     return 0;
}


The ternary conditional expression is just as simple and verbose as your code, and a whole lot shorter.

return (!front) ? 1 : 0;


But since we are returning whether this function is true or not, we don't even have a need for the conditional expression at all.

return !front;


-
You don't accept any parameters for some of your functions, such as main(). If you do not take in any parameters, declare them as void.

int main(void)


-
You have a comparison between an int and a void *.

peek() == NULL


Compare to 0 instead.

0 == peek()


Indentation

-
You are very inconsistent with your indenting.

if(front == NULL)
front = tmp;
else
rear -> link = tmp;
rear = tmp;


This isn't very readable. For one, you don't include braces, which is a whole 'nother debate within itself, but then you don't use indentation. Please do everyone that is trying to read your code a favor, and indent your code properly.

Comments:

-
Comments are very important to have in your source code. Your program has a lack of comments that explain why your code does what it does. You should add some so you other people can follow your code more easily.

-
For the comments you do have...

```
//printf("%d\n", strlen(name));

/*if(i == 28 && name[0] != '\0')
{
fo

Code Snippets

tmp = (struct node *)malloc(sizeof(struct node));
*front = NULL, *rear = NULL;
char name[4] = "\0";
char name[4] = {0};
inline int isEmpty(Node front)

Context

StackExchange Code Review Q#43202, answer score: 15

Revisions (0)

No revisions yet.