So i was looking at octal/hexa constants in C, for example
char a='\100';
unsigned char b='\xff';
The standard says their max value (of the octal/hexa escape sequences) is equal to a max value of a unsigned character, so that means 255.
What im writing is a syntactic checker (debuger) which will report an error/warning if the constant is not written correctly... and im stuck on this part.
Code:
while((c=getchar())!=EOF)
{
...
else if(state==squote_escape) /* after it reads a '\ */
{
....
}
}
Basicaly, i dont know how to add the octal constant check.
If anyone can come up with a pseudocode it would be nice.
Code:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 1024
/* push, pop, check function */
char stack[MAX_SIZE];
int stack_counter=0;
int push(char c) /* pushes character to top of the stack */
{
if(stack_counter==1024) return 0;
stack[stack_counter++]=c;
return 1;
}
int pop() /* decrements stack_counter */
{
if(stack_counter<0) return 0;
stack_counter--;
return 1;
}
int check (char c)
{
char tmp;
switch(c)
{
case '(':
case '[':
case '{':
if((tmp=push(c)==0)) return 0;
break;
case ')':
tmp=pop();
if(tmp==0) return 0;
if(stack[stack_counter]!='(') return 0;
break;
case ']':
tmp=pop();
if(tmp==0) return 0;
if(stack[stack_counter]!='[') return 0;
break;
case '}':
tmp=pop();
if(tmp==0) return 0;
if(stack[stack_counter]!='{') return 0;
break;
}
return 1;
}
int main(int argc, char *argv[])
{
int c;
enum states { normal,
comment_entry, comment, comment_exit,
squote, squote2, squote3, squote_escape,
dquote, dquote_escape };
int line=1;
int state=normal;
int x=0;
int valid=1;
char tmp=0;
while((c=getchar())!=EOF)
{
if(state==normal && c=='/') { state = comment_entry; }
else if(state==normal && c=='"') { state = dquote; }
else if(state==normal && c=='\'') { state = squote; }
else if(state==normal) { if((check(c))==0) {printf("Line %d: Mismatching %c\n", line, c); valid=0;} }
else if(state==comment_entry && c=='*') { state = comment; }
else if(state==comment_entry && c=='/') { }
else if(state==comment_entry) { if(c=='"') { state = dquote; } else if(c=='\'') { state = squote; } else {state = normal; if((check(c))==0) {printf("Line %d: Mismatching %c\n", line, c); valid=0; } } } /* neccesary to check braces here aswell */
else if(state==comment && c=='*') { state = comment_exit; }
else if(state==comment) {}
else if(state==comment_exit && c=='*') {}
else if(state==comment_exit && c=='/') { state = normal; }
else if(state==comment_exit) { state = comment; }
else if(state==dquote && c=='"') { state = normal; }
else if(state==dquote && c=='\n') { state = normal; printf("Line %d: Double quotes not properly closed.\n", line); valid=0; }
else if(state==dquote && c=='\\') { state = dquote_escape; }
else if(state==dquote) { }
else if(state==dquote_escape) { state = dquote; }
else if(state==squote && (c=='\'' || c=='\n')) { state = normal; printf("Line %d: Constant not properly closed.\n", line); valid=0; }
else if(state==squote && c=='\\') { state = squote_escape; }
else if(state==squote) { state = squote2; }
else if(state==squote_escape && (c=='\'' || c=='"' || c=='?' || c=='\\' || c=='a' || c=='b' || c=='f' || c=='n' || c=='r' || c=='t' || c=='v')) { state = squote2; }
else if(state==squote_escape) { printf("[Warning]: Line %d: Unknown escape sequence.\n", line); state = squote2; }
else if(state==squote2 && c=='\'') { state = normal; }
else if(state==squote2) { state = squote3; valid=0; printf("Line %d: Constant not properly closed.\n", line); }
else if(state==squote3 && c=='\\') { tmp=1; }
else if(state==squote3 && tmp==1) { tmp = 0; }
else if(state==squote3 && (c=='\'' || c=='\n')) { state = normal; }
if(c=='\n') line++;
}
/* promijenjeno squote stanje */
for(x=0; x<stack_counter; x++)
printf("Mismatching %c\n", stack[x]);
switch(state)
{
case comment:
case comment_exit:
printf("Code ends inside a comment.\n"); valid=0; break;
case squote:
case squote2:
case squote3:
case squote_escape:
printf("Code ends inside '.\n"); valid=0; break;
}
if(valid==1)
printf("No syntactic errors.\n");
printf("Press any key to continue");
getchar();
return 0;
}