티스토리 뷰
목차
반응형
c Minus Scanner 소스, c마이너스 스캐너 예제 (C++ Lex 응용)
c Minus Scanner 스캐너 소스 소개하기에 앞서, 먼저 C Minus의 어휘 규칙을 설명합니다.
C Minus의 어휘 규칙.
키워드
else if int return void while
특수 심볼
+ - * / < <= > >= == != = ; , ( ) [ ] { } /- *-
문자
소문자와 대문자는 서로 다른 문자로 취급
공백 문자
빈자리, 줄바꿈, 탭이 존재하며 보통은 무시하나 ID와 NUM 가운데 공백 문자가 있어야 분리됨
주석
/-와 *-로 둘러싸며, 한 줄 이상이어도 되지만 중첩될 수는 없음
첨부 파일 : c-minus scanner.c
아래는 위 첨부 파일(c마이너스 스캐너)의 전체 소스입니다.
[c마이너스 스캐너 소스] 이런 스캐너 제품과는 관련이 없습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | /* Copyright (c) 2008-2009 LordZen This file is part of tinycalc. tinycalc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. tinycalc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with tinycalc. If not, see <http://www.gnu.org/licenses/>. */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include "config.h" #include "debug.h" #include "lex.h" #include "convert.h" #include "parser.h" void usage(char *progname) { printf("\n:: Tiny Calculator %s ::\n",PACKAGE_VERSION); printf("Copyright (c) 2008-2009 LordZen\n\n"); printf("USAGE: %s <expression>\n",progname); printf("SAMPLE EXPRESSION: 0x2f+57-(0b1100001/4)\n\n"); exit(0); } void show_debug(char *s) { #ifdef DEBUG printf("[*] DEBUG: %s\n",s); #endif } void show_error(char *s) { printf("\n[**] FATAL: %s\n\n",s); exit(-1); } long hex2dec(char *num) { //skip 0x int i = 2; int pos = strlen(num) - 3; long curr_num = 0; long tot = 0; while(num[i] != '\0') { switch(num[i]) { case '0': curr_num = 0; break; case '1': curr_num = 1; break; case '2': curr_num = 2; break; case '3': curr_num = 3; break; case '4': curr_num = 4; break; case '5': curr_num = 5; break; case '6': curr_num = 6; break; case '7': curr_num = 7; break; case '8': curr_num = 8; break; case '9': curr_num = 9; break; case 'a': case 'A': curr_num = 10; break; case 'b': case 'B': curr_num = 11; break; case 'c': case 'C': curr_num = 12; break; case 'd': case 'D': curr_num = 13; break; case 'e': case 'E': curr_num = 14; break; case 'f': case 'F': curr_num = 15; break; } tot += curr_num * pow(16,pos--); i++; } return tot; } long bin2dec(char *num) { //skip 0b int i = 2; int pos = strlen(num) - 3; long curr_num = 0; long tot = 0; while(num[i] != '\0') { switch(num[i]) { case '0': curr_num = 0; break; case '1': curr_num = 1; break; } tot += curr_num * pow(2,pos--); i++; } return tot; } void dec2bin(unsigned long num,char *bin) { int i = 0; char buffer[MAX_BIT_BIN+1]; //pointer to the end of buffer char *pbuf = buffer + sizeof(buffer) - 1; *pbuf-- = '\0'; for(i=0; i<sizeof(num)*8; ++i) { unsigned long bit = (unsigned int)1 << i; *pbuf-- = ((num & bit)? '1' : '0'); } *pbuf = ' '; //skip leading spaces and zeros while(*pbuf == '0' || *pbuf == ' ') pbuf++; strcpy(bin,pbuf); } void dec2hex(unsigned long num,char *hex) { char buffer[MAX_BIT_HEX+1]; //pointer to the end of buffer char *pbuf = buffer +sizeof(buffer) - 1; *pbuf-- = '\0'; while(num != 0) { switch(num % 16) { case 0: *pbuf-- = '0'; break; case 1: *pbuf-- = '1'; break; case 2: *pbuf-- = '2'; break; case 3: *pbuf-- = '3'; break; case 4: *pbuf-- = '4'; break; case 5: *pbuf-- = '5'; break; case 6: *pbuf-- = '6'; break; case 7: *pbuf-- = '7'; break; case 8: *pbuf-- = '8'; break; case 9: *pbuf-- = '9'; break; case 10: *pbuf-- = 'a'; break; case 11: *pbuf-- = 'b'; break; case 12: *pbuf-- = 'c'; break; case 13: *pbuf-- = 'd'; break; case 14: *pbuf-- = 'e'; break; case 15: *pbuf-- = 'f'; break; } num /= 16; } pbuf++; strcpy(hex,pbuf); } int main(int argc,char *argv[]) { int decres = 0; char binres[MAX_BIT_BIN+1]; char hexres[MAX_BIT_HEX+1]; if(argc != 2) usage(argv[0]); Lex *lex = lex_init(argv[1]); if(lex == NULL) show_error("syntax analizator not initialized"); decres = parse(lex); lex_free(lex); //convert result in binary format dec2bin((unsigned int)abs(decres),binres); //convert result in esadecimal format dec2hex((unsigned int)abs(decres),hexres); //print the result if(decres < 0) printf("\n\t%d\t\t\t-0x%s\t\t\t-0b%s\n\n",decres,hexres,binres); else printf("\n\t%d\t\t\t0x%s\t\t\t0b%s\n\n",decres,hexres,binres); return 0; } | cs |
c Minus Scanner 소스, c마이너스 스캐너 예제 (C++ Lex 응용)
반응형