Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

octal and hexadecimal int added

  • Loading branch information...
commit a26536143c21b199ed44980828f138a2154ba4a5 1 parent 701747a
@weitong weitong authored
Showing with 45 additions and 7 deletions.
  1. +8 −0 Makefile
  2. +21 −4 lexical.l
  3. +3 −3 tree.h
  4. +13 −0 types.h
View
8 Makefile
@@ -4,3 +4,11 @@ syntax.tab.c: syntax.y lexical.l
bison -d -v syntax.y
lex.yy.c: lexical.l
flex lexical.l
+
+clean:
+ rm syntax.tab.c
+ rm syntax.tab.h
+ rm lex.yy.c
+ rm parser.out
+ rm syntax.output
+ rm scanner.out
View
25 lexical.l
@@ -12,7 +12,9 @@ struct TreeNode *create_token_node(char *name, TypeTag tag, char *value);
DIGIT [0-9]
LETTER [a-zA-Z_]
-INT {DIGIT}+
+INT 0|[1-9]{DIGIT}*
+OCT 0([0-7]+)
+HEX 0(x|X)[0-9a-fA-F]+
FLOAT {DIGIT}+(\.{DIGIT}+)?((E|e)[+-]?{DIGIT}+)?
ID {LETTER}({LETTER}|{DIGIT})*
DELIM [ \t]
@@ -185,6 +187,24 @@ LINE [\n]
return INT;
}
+{OCT} {
+ printf("LEXICAL TOKEN: OCT --- %d\n", oct_to_i(yytext));
+ char *a = (char *)malloc(20);
+ sprintf(a, "%d", oct_to_i(yytext));
+ yylval.node_value = create_token_node("INT", INT_T, a);
+ free(a);
+ return INT;
+}
+
+{HEX} {
+ printf("LEXICAL TOKEN: HEX --- %d\n", hex_to_i(yytext));
+ char *a = (char *)malloc(20);
+ sprintf(a, "%d", hex_to_i(yytext));
+ yylval.node_value = create_token_node("INT", INT_T, a);
+ free(a);
+ return INT;
+}
+
{FLOAT} {
printf("LEXICAL TOKEN: FLOAT --- %f\n", atof(yytext));
yylval.node_value = create_token_node("FLOAT", FLOAT_T, yytext);
@@ -208,15 +228,12 @@ struct TreeNode *create_token_node(char *name, enum TypeTag tag, char *value){
(p->content).name = name;
if (STR_T == tag) {
(p->content).str_value = value;
- // p->str_value = value;
}
else if(INT_T == tag) {
(p->content).int_value = atoi(value);
- //p->int_value = atoi(value);
}
else if(FLOAT_T == tag) {
(p->content).float_value = atof(value);
- //p->float_value = atof(value);
}
return p;
}
View
6 tree.h
@@ -72,7 +72,7 @@ NodePtr insert_child(NodePtr parent, NodePtr child) {
child_tmp = parent->first_child;
if (NULL == child_tmp) {
- printf("~~~~~~~~~~~~~~~~~~~0\n");
+ // printf("~~~~~~~~~~~~~~~~~~~0\n");
parent->first_child = child;
return parent;
}
@@ -82,9 +82,9 @@ NodePtr insert_child(NodePtr parent, NodePtr child) {
child_tmp = child_tmp->next_sibling;
i ++ ;
}
- printf("~~~~~~~~~~~~~~~insert at : %d \n", i);
+ //printf("~~~~~~~~~~~~~~~insert at : %d \n", i);
child_pose->next_sibling = child;
- printf("~~~~~~~~~~~name: %s\n", child_pose->next_sibling->content.name);
+ //printf("~~~~~~~~~~~name: %s\n", child_pose->next_sibling->content.name);
return parent;
}
View
13 types.h
@@ -8,4 +8,17 @@ typedef short int_16;
typedef unsigned char uint_8;
typedef char int_8;
+int_32 oct_to_i(char *oct) {
+ int n = *oct - '0';
+ while (*(++oct) != '\0') n = n*8 + *oct - '0';
+ return n;
+}
+
+int_32 hex_to_i(char *hex) {
+ hex += 2;
+ int n = *hex - '0';
+ while (*(++hex) != '\0') n = n*16 + *hex - '0';
+ return n;
+}
+
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.