├── Compiler.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── Compiler.xccheckout │ └── xcuserdata │ │ └── xiaojia.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── xiaojia.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── Compiler.xcscheme │ └── xcschememanagement.plist ├── Compiler ├── Compiler-Prefix.pch ├── Compiler.1 ├── LexicalAnalysis.c └── options.txt ├── README.md └── options.txt /Compiler.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 9166E75B18FD6038000AAAF4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9166E75A18FD6038000AAAF4 /* Foundation.framework */; }; 11 | 9166E76218FD6038000AAAF4 /* Compiler.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 9166E76118FD6038000AAAF4 /* Compiler.1 */; }; 12 | 9166E76918FD6076000AAAF4 /* LexicalAnalysis.c in Sources */ = {isa = PBXBuildFile; fileRef = 9166E76818FD6076000AAAF4 /* LexicalAnalysis.c */; }; 13 | /* End PBXBuildFile section */ 14 | 15 | /* Begin PBXCopyFilesBuildPhase section */ 16 | 9166E75518FD6038000AAAF4 /* CopyFiles */ = { 17 | isa = PBXCopyFilesBuildPhase; 18 | buildActionMask = 2147483647; 19 | dstPath = /usr/share/man/man1/; 20 | dstSubfolderSpec = 0; 21 | files = ( 22 | 9166E76218FD6038000AAAF4 /* Compiler.1 in CopyFiles */, 23 | ); 24 | runOnlyForDeploymentPostprocessing = 1; 25 | }; 26 | /* End PBXCopyFilesBuildPhase section */ 27 | 28 | /* Begin PBXFileReference section */ 29 | 912AA7B2191FD44000357C8A /* options.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = options.txt; sourceTree = ""; }; 30 | 9166E75718FD6038000AAAF4 /* Compiler */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Compiler; sourceTree = BUILT_PRODUCTS_DIR; }; 31 | 9166E75A18FD6038000AAAF4 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; 32 | 9166E76018FD6038000AAAF4 /* Compiler-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Compiler-Prefix.pch"; sourceTree = ""; }; 33 | 9166E76118FD6038000AAAF4 /* Compiler.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = Compiler.1; sourceTree = ""; }; 34 | 9166E76818FD6076000AAAF4 /* LexicalAnalysis.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = LexicalAnalysis.c; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | 9166E75418FD6038000AAAF4 /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | 9166E75B18FD6038000AAAF4 /* Foundation.framework in Frameworks */, 43 | ); 44 | runOnlyForDeploymentPostprocessing = 0; 45 | }; 46 | /* End PBXFrameworksBuildPhase section */ 47 | 48 | /* Begin PBXGroup section */ 49 | 9166E74E18FD6038000AAAF4 = { 50 | isa = PBXGroup; 51 | children = ( 52 | 9166E75C18FD6038000AAAF4 /* Compiler */, 53 | 9166E75918FD6038000AAAF4 /* Frameworks */, 54 | 9166E75818FD6038000AAAF4 /* Products */, 55 | ); 56 | sourceTree = ""; 57 | }; 58 | 9166E75818FD6038000AAAF4 /* Products */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 9166E75718FD6038000AAAF4 /* Compiler */, 62 | ); 63 | name = Products; 64 | sourceTree = ""; 65 | }; 66 | 9166E75918FD6038000AAAF4 /* Frameworks */ = { 67 | isa = PBXGroup; 68 | children = ( 69 | 9166E75A18FD6038000AAAF4 /* Foundation.framework */, 70 | ); 71 | name = Frameworks; 72 | sourceTree = ""; 73 | }; 74 | 9166E75C18FD6038000AAAF4 /* Compiler */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | 9166E76118FD6038000AAAF4 /* Compiler.1 */, 78 | 9166E76818FD6076000AAAF4 /* LexicalAnalysis.c */, 79 | 912AA7B2191FD44000357C8A /* options.txt */, 80 | 9166E75F18FD6038000AAAF4 /* Supporting Files */, 81 | ); 82 | path = Compiler; 83 | sourceTree = ""; 84 | }; 85 | 9166E75F18FD6038000AAAF4 /* Supporting Files */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | 9166E76018FD6038000AAAF4 /* Compiler-Prefix.pch */, 89 | ); 90 | name = "Supporting Files"; 91 | sourceTree = ""; 92 | }; 93 | /* End PBXGroup section */ 94 | 95 | /* Begin PBXNativeTarget section */ 96 | 9166E75618FD6038000AAAF4 /* Compiler */ = { 97 | isa = PBXNativeTarget; 98 | buildConfigurationList = 9166E76518FD6038000AAAF4 /* Build configuration list for PBXNativeTarget "Compiler" */; 99 | buildPhases = ( 100 | 9166E75318FD6038000AAAF4 /* Sources */, 101 | 9166E75418FD6038000AAAF4 /* Frameworks */, 102 | 9166E75518FD6038000AAAF4 /* CopyFiles */, 103 | ); 104 | buildRules = ( 105 | ); 106 | dependencies = ( 107 | ); 108 | name = Compiler; 109 | productName = Compiler; 110 | productReference = 9166E75718FD6038000AAAF4 /* Compiler */; 111 | productType = "com.apple.product-type.tool"; 112 | }; 113 | /* End PBXNativeTarget section */ 114 | 115 | /* Begin PBXProject section */ 116 | 9166E74F18FD6038000AAAF4 /* Project object */ = { 117 | isa = PBXProject; 118 | attributes = { 119 | LastUpgradeCheck = 0510; 120 | ORGANIZATIONNAME = sirius; 121 | }; 122 | buildConfigurationList = 9166E75218FD6038000AAAF4 /* Build configuration list for PBXProject "Compiler" */; 123 | compatibilityVersion = "Xcode 3.2"; 124 | developmentRegion = English; 125 | hasScannedForEncodings = 0; 126 | knownRegions = ( 127 | en, 128 | ); 129 | mainGroup = 9166E74E18FD6038000AAAF4; 130 | productRefGroup = 9166E75818FD6038000AAAF4 /* Products */; 131 | projectDirPath = ""; 132 | projectRoot = ""; 133 | targets = ( 134 | 9166E75618FD6038000AAAF4 /* Compiler */, 135 | ); 136 | }; 137 | /* End PBXProject section */ 138 | 139 | /* Begin PBXSourcesBuildPhase section */ 140 | 9166E75318FD6038000AAAF4 /* Sources */ = { 141 | isa = PBXSourcesBuildPhase; 142 | buildActionMask = 2147483647; 143 | files = ( 144 | 9166E76918FD6076000AAAF4 /* LexicalAnalysis.c in Sources */, 145 | ); 146 | runOnlyForDeploymentPostprocessing = 0; 147 | }; 148 | /* End PBXSourcesBuildPhase section */ 149 | 150 | /* Begin XCBuildConfiguration section */ 151 | 9166E76318FD6038000AAAF4 /* Debug */ = { 152 | isa = XCBuildConfiguration; 153 | buildSettings = { 154 | ALWAYS_SEARCH_USER_PATHS = NO; 155 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 156 | CLANG_CXX_LIBRARY = "libc++"; 157 | CLANG_ENABLE_MODULES = YES; 158 | CLANG_ENABLE_OBJC_ARC = YES; 159 | CLANG_WARN_BOOL_CONVERSION = YES; 160 | CLANG_WARN_CONSTANT_CONVERSION = YES; 161 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 162 | CLANG_WARN_EMPTY_BODY = YES; 163 | CLANG_WARN_ENUM_CONVERSION = YES; 164 | CLANG_WARN_INT_CONVERSION = YES; 165 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 166 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 167 | COPY_PHASE_STRIP = NO; 168 | GCC_C_LANGUAGE_STANDARD = gnu99; 169 | GCC_DYNAMIC_NO_PIC = NO; 170 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 171 | GCC_OPTIMIZATION_LEVEL = 0; 172 | GCC_PREPROCESSOR_DEFINITIONS = ( 173 | "DEBUG=1", 174 | "$(inherited)", 175 | ); 176 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 177 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 178 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 179 | GCC_WARN_UNDECLARED_SELECTOR = YES; 180 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 181 | GCC_WARN_UNUSED_FUNCTION = YES; 182 | GCC_WARN_UNUSED_VARIABLE = YES; 183 | MACOSX_DEPLOYMENT_TARGET = 10.9; 184 | ONLY_ACTIVE_ARCH = YES; 185 | SDKROOT = macosx; 186 | }; 187 | name = Debug; 188 | }; 189 | 9166E76418FD6038000AAAF4 /* Release */ = { 190 | isa = XCBuildConfiguration; 191 | buildSettings = { 192 | ALWAYS_SEARCH_USER_PATHS = NO; 193 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 194 | CLANG_CXX_LIBRARY = "libc++"; 195 | CLANG_ENABLE_MODULES = YES; 196 | CLANG_ENABLE_OBJC_ARC = YES; 197 | CLANG_WARN_BOOL_CONVERSION = YES; 198 | CLANG_WARN_CONSTANT_CONVERSION = YES; 199 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 200 | CLANG_WARN_EMPTY_BODY = YES; 201 | CLANG_WARN_ENUM_CONVERSION = YES; 202 | CLANG_WARN_INT_CONVERSION = YES; 203 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 204 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 205 | COPY_PHASE_STRIP = YES; 206 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 207 | ENABLE_NS_ASSERTIONS = NO; 208 | GCC_C_LANGUAGE_STANDARD = gnu99; 209 | GCC_ENABLE_OBJC_EXCEPTIONS = YES; 210 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 211 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 212 | GCC_WARN_UNDECLARED_SELECTOR = YES; 213 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 214 | GCC_WARN_UNUSED_FUNCTION = YES; 215 | GCC_WARN_UNUSED_VARIABLE = YES; 216 | MACOSX_DEPLOYMENT_TARGET = 10.9; 217 | SDKROOT = macosx; 218 | }; 219 | name = Release; 220 | }; 221 | 9166E76618FD6038000AAAF4 /* Debug */ = { 222 | isa = XCBuildConfiguration; 223 | buildSettings = { 224 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 225 | GCC_PREFIX_HEADER = "Compiler/Compiler-Prefix.pch"; 226 | PRODUCT_NAME = "$(TARGET_NAME)"; 227 | }; 228 | name = Debug; 229 | }; 230 | 9166E76718FD6038000AAAF4 /* Release */ = { 231 | isa = XCBuildConfiguration; 232 | buildSettings = { 233 | GCC_PRECOMPILE_PREFIX_HEADER = YES; 234 | GCC_PREFIX_HEADER = "Compiler/Compiler-Prefix.pch"; 235 | PRODUCT_NAME = "$(TARGET_NAME)"; 236 | }; 237 | name = Release; 238 | }; 239 | /* End XCBuildConfiguration section */ 240 | 241 | /* Begin XCConfigurationList section */ 242 | 9166E75218FD6038000AAAF4 /* Build configuration list for PBXProject "Compiler" */ = { 243 | isa = XCConfigurationList; 244 | buildConfigurations = ( 245 | 9166E76318FD6038000AAAF4 /* Debug */, 246 | 9166E76418FD6038000AAAF4 /* Release */, 247 | ); 248 | defaultConfigurationIsVisible = 0; 249 | defaultConfigurationName = Release; 250 | }; 251 | 9166E76518FD6038000AAAF4 /* Build configuration list for PBXNativeTarget "Compiler" */ = { 252 | isa = XCConfigurationList; 253 | buildConfigurations = ( 254 | 9166E76618FD6038000AAAF4 /* Debug */, 255 | 9166E76718FD6038000AAAF4 /* Release */, 256 | ); 257 | defaultConfigurationIsVisible = 0; 258 | defaultConfigurationName = Release; 259 | }; 260 | /* End XCConfigurationList section */ 261 | }; 262 | rootObject = 9166E74F18FD6038000AAAF4 /* Project object */; 263 | } 264 | -------------------------------------------------------------------------------- /Compiler.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Compiler.xcodeproj/project.xcworkspace/xcshareddata/Compiler.xccheckout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDESourceControlProjectFavoriteDictionaryKey 6 | 7 | IDESourceControlProjectIdentifier 8 | CAFF12B9-965C-489E-AAB1-54174C903D0E 9 | IDESourceControlProjectName 10 | Compiler 11 | IDESourceControlProjectOriginsDictionary 12 | 13 | 0508668B-1588-48EB-BC96-2BD0DC9B01CF 14 | https://github.com/YuJianSirius/Compiler.git 15 | 16 | IDESourceControlProjectPath 17 | Compiler.xcodeproj/project.xcworkspace 18 | IDESourceControlProjectRelativeInstallPathDictionary 19 | 20 | 0508668B-1588-48EB-BC96-2BD0DC9B01CF 21 | ../.. 22 | 23 | IDESourceControlProjectURL 24 | https://github.com/YuJianSirius/shutterbug.git 25 | IDESourceControlProjectVersion 26 | 110 27 | IDESourceControlProjectWCCIdentifier 28 | 0508668B-1588-48EB-BC96-2BD0DC9B01CF 29 | IDESourceControlProjectWCConfigurations 30 | 31 | 32 | IDESourceControlRepositoryExtensionIdentifierKey 33 | public.vcs.git 34 | IDESourceControlWCCIdentifierKey 35 | 0508668B-1588-48EB-BC96-2BD0DC9B01CF 36 | IDESourceControlWCCName 37 | Compiler 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /Compiler.xcodeproj/project.xcworkspace/xcuserdata/xiaojia.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YuJianSirius/Compiler/802227144dfb8bdcb3a6bfa9de606953597bfdfd/Compiler.xcodeproj/project.xcworkspace/xcuserdata/xiaojia.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Compiler.xcodeproj/xcuserdata/xiaojia.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 18 | 19 | 20 | 22 | 32 | 33 | 34 | 36 | 46 | 47 | 48 | 50 | 60 | 61 | 62 | 64 | 76 | 77 | 78 | 80 | 92 | 93 | 94 | 96 | 106 | 107 | 108 | 110 | 122 | 123 | 124 | 126 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /Compiler.xcodeproj/xcuserdata/xiaojia.xcuserdatad/xcschemes/Compiler.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 51 | 52 | 58 | 59 | 60 | 61 | 62 | 63 | 69 | 70 | 76 | 77 | 78 | 79 | 81 | 82 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /Compiler.xcodeproj/xcuserdata/xiaojia.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Compiler.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 9166E75618FD6038000AAAF4 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Compiler/Compiler-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #ifdef __OBJC__ 8 | #import 9 | #endif 10 | -------------------------------------------------------------------------------- /Compiler/Compiler.1: -------------------------------------------------------------------------------- 1 | .\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples. 2 | .\"See Also: 3 | .\"man mdoc.samples for a complete listing of options 4 | .\"man mdoc for the short list of editing options 5 | .\"/usr/share/misc/mdoc.template 6 | .Dd 4/15/14 \" DATE 7 | .Dt Compiler 1 \" Program name and manual section number 8 | .Os Darwin 9 | .Sh NAME \" Section Header - required - don't modify 10 | .Nm Compiler, 11 | .\" The following lines are read in generating the apropos(man -k) database. Use only key 12 | .\" words here as the database is built based on the words here and in the .ND line. 13 | .Nm Other_name_for_same_program(), 14 | .Nm Yet another name for the same program. 15 | .\" Use .Nm macro to designate other names for the documented program. 16 | .Nd This line parsed for whatis database. 17 | .Sh SYNOPSIS \" Section Header - required - don't modify 18 | .Nm 19 | .Op Fl abcd \" [-abcd] 20 | .Op Fl a Ar path \" [-a path] 21 | .Op Ar file \" [file] 22 | .Op Ar \" [file ...] 23 | .Ar arg0 \" Underlined argument - use .Ar anywhere to underline 24 | arg2 ... \" Arguments 25 | .Sh DESCRIPTION \" Section Header - required - don't modify 26 | Use the .Nm macro to refer to your program throughout the man page like such: 27 | .Nm 28 | Underlining is accomplished with the .Ar macro like this: 29 | .Ar underlined text . 30 | .Pp \" Inserts a space 31 | A list of items with descriptions: 32 | .Bl -tag -width -indent \" Begins a tagged list 33 | .It item a \" Each item preceded by .It macro 34 | Description of item a 35 | .It item b 36 | Description of item b 37 | .El \" Ends the list 38 | .Pp 39 | A list of flags and their descriptions: 40 | .Bl -tag -width -indent \" Differs from above in tag removed 41 | .It Fl a \"-a flag as a list item 42 | Description of -a flag 43 | .It Fl b 44 | Description of -b flag 45 | .El \" Ends the list 46 | .Pp 47 | .\" .Sh ENVIRONMENT \" May not be needed 48 | .\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1 49 | .\" .It Ev ENV_VAR_1 50 | .\" Description of ENV_VAR_1 51 | .\" .It Ev ENV_VAR_2 52 | .\" Description of ENV_VAR_2 53 | .\" .El 54 | .Sh FILES \" File used or created by the topic of the man page 55 | .Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact 56 | .It Pa /usr/share/file_name 57 | FILE_1 description 58 | .It Pa /Users/joeuser/Library/really_long_file_name 59 | FILE_2 description 60 | .El \" Ends the list 61 | .\" .Sh DIAGNOSTICS \" May not be needed 62 | .\" .Bl -diag 63 | .\" .It Diagnostic Tag 64 | .\" Diagnostic informtion here. 65 | .\" .It Diagnostic Tag 66 | .\" Diagnostic informtion here. 67 | .\" .El 68 | .Sh SEE ALSO 69 | .\" List links in ascending order by section, alphabetically within a section. 70 | .\" Please do not reference files that do not exist without filing a bug report 71 | .Xr a 1 , 72 | .Xr b 1 , 73 | .Xr c 1 , 74 | .Xr a 2 , 75 | .Xr b 2 , 76 | .Xr a 3 , 77 | .Xr b 3 78 | .\" .Sh BUGS \" Document known, unremedied bugs 79 | .\" .Sh HISTORY \" Document history if command behaves in a unique manner -------------------------------------------------------------------------------- /Compiler/LexicalAnalysis.c: -------------------------------------------------------------------------------- 1 | // 2 | // LexicalAnalysis.c 3 | // Compiler 4 | // 5 | // Created by xiaojia on 4/15/14. 6 | // Copyright (c) 2014 sirius. All rights reserved. 7 | // 8 | 9 | #include 10 | #include 11 | #include "string.h" 12 | 13 | // ---------------- 关键词表 ---------------------- 14 | char *keyword[] = {"auto","double","int","struct","break","else","long","switch","case","enum","register", 15 | "typedef","char","extern","return","union","const","float","short","unsigned","continue","for", 16 | "signed","void","default","goto","sizeof","volatile","do","while","static","if"}; 17 | 18 | // ---------------- 运算符表 ---------------------- 19 | char *operator[] = {"+", "-" ,"*", "/" , "<" ,">"}; 20 | 21 | // ---------------- 限界符表 ---------------------- 22 | char *boundword[] = {"=",";",",","\"",":","(",")"}; 23 | 24 | //关键词种别码 25 | #define AUTO 1 26 | #define DOUBLE 2 27 | #define INT 3 28 | #define STRUCT 4 29 | #define BREAK 5 30 | #define ELSE 6 31 | #define LONG 7 32 | #define SWITCH 8 33 | #define CASE 9 34 | #define ENUM 10 35 | #define REGISTER 11 36 | #define TYPEDEF 12 37 | #define CHAR 13 38 | #define EXTERN 14 39 | #define RETURN 15 40 | #define UNION 16 41 | #define CONST 17 42 | #define FLOAT 18 43 | #define SHORT 19 44 | #define UNSIGNED 20 45 | #define CONTINUE 21 46 | #define FOR 22 47 | #define SIGNED 23 48 | #define VOID 24 49 | #define DEFAULT 25 50 | #define GOTO 26 51 | #define SIZEOF 27 52 | #define VOLATITLE 28 53 | #define DO 29 54 | #define WHILE 30 55 | #define STATIC 31 56 | #define IF 32 57 | #define KEY_DESC "关键字" 58 | 59 | //标识符种别码 60 | #define IDENTIFER 40 61 | #define IDENTIFER_DESC "标识符" 62 | 63 | //常量标种别码 64 | #define INT_VAL 51 //整形常量 65 | #define CHAR_VAL 52 //字符常量 66 | #define FLOAT_VAL 53 //单精度浮点数常量 67 | #define DOUBEL_VAL 54 //双精度浮点数常量 68 | #define MACRO_VAL 55 //宏常量 69 | #define CONSTANT_DESC "常量的" 70 | 71 | //运算符 72 | #define ADD 60 //+ 73 | #define SUB 61 //- 74 | #define MUL 62 //* 75 | #define DIV 63 /// 76 | #define LESS_THAN 64 //< 77 | #define GTEATER_THAN 65 //> 78 | #define OPERATOR_DESC "运算符" 79 | 80 | //限界符 81 | #define EQU 100 //= 82 | #define SEM 101 //; 83 | #define COM 102 //, 84 | #define DOUBLE_QUO 103 //" 85 | #define COL 104 //: 86 | #define LEFT_PAR 105 //( 87 | #define RIGHT_PAR 106 //) 88 | #define BOUNTWROD_DESC "限界符" 89 | 90 | #define NOTE1 150 // //注释// 91 | #define NOTE2 151 // /*注释*/ 92 | #define NOTE_DESC "注释" 93 | 94 | #define HEADER 160 // 头文件 95 | #define HEADER_DESC "头文件" 96 | 97 | #define MAX_CHAR_NUMBER 100000 98 | char charSET[MAX_CHAR_NUMBER]; 99 | 100 | typedef struct NormalNode 101 | { 102 | char content[50]; 103 | char description[30]; 104 | int type; 105 | int address; 106 | int line; 107 | struct NormalNode *next; 108 | }NormalNode; 109 | 110 | typedef struct IdentiferNode 111 | { 112 | char content[50]; 113 | char description[30]; 114 | int type; 115 | int address; 116 | int line; 117 | struct IdentiferNode *next; 118 | }IdentiferNode; 119 | 120 | 121 | NormalNode *normalHead; 122 | IdentiferNode *identiferHead; 123 | 124 | void initNode() 125 | { 126 | normalHead = (NormalNode *)malloc(sizeof(NormalNode)); 127 | strcpy(normalHead->content, ""); 128 | strcpy(normalHead->description, ""); 129 | normalHead->type = -1; 130 | normalHead->address = -1; 131 | normalHead->line = -1; 132 | normalHead->next = NULL; 133 | 134 | identiferHead = (IdentiferNode *)malloc(sizeof(IdentiferNode)); 135 | strcpy(identiferHead->content, ""); 136 | strcpy(identiferHead->description, ""); 137 | identiferHead->type = -1; 138 | identiferHead->address = -1; 139 | identiferHead->line = -1; 140 | identiferHead->next = NULL; 141 | } 142 | 143 | void createNewNode(char *content,char *describe,int type,int address,int line) 144 | { 145 | NormalNode *p = normalHead; 146 | NormalNode *tmp = (NormalNode *)malloc(sizeof(NormalNode)); 147 | 148 | while ( p->next != NULL) { 149 | p = p->next; 150 | } 151 | 152 | strcpy(tmp->content,content); 153 | strcpy(tmp->description,describe); 154 | tmp->type = type; 155 | tmp->address = address; 156 | tmp->line = line; 157 | tmp->next = NULL; 158 | 159 | p->next = tmp; 160 | } 161 | 162 | int createIdeNode(char *content,char *describe,int type,int address,int line) 163 | { 164 | IdentiferNode *p = identiferHead; 165 | IdentiferNode *tmp = (IdentiferNode *)malloc(sizeof(IdentiferNode)); 166 | 167 | int address_count = 1; 168 | 169 | while (p->next != NULL){ 170 | p = p -> next; 171 | address_count++; 172 | if(!strcmp(p->content, content)) //标识符已经存在 173 | return p->address; 174 | } 175 | strcpy(tmp->content,content); 176 | strcpy(tmp->description,describe); 177 | tmp->type = type; 178 | tmp->address = address_count; 179 | tmp->line = line; 180 | tmp->next = NULL; 181 | 182 | p->next= tmp; 183 | 184 | return tmp->address; 185 | } 186 | 187 | void displaynormalNode() 188 | { 189 | NormalNode *p = normalHead; 190 | 191 | printf("%6内容%10描述%8种别码%5内存地址%6行号\n"); 192 | 193 | while ( (p = p -> next)) { 194 | printf("%10s",p->content); 195 | printf("%15s",p->description); 196 | printf("%10d",p->type); 197 | if (p->address!=-1) { 198 | printf("%10d",p->address); 199 | }else{ 200 | printf(" "); 201 | } 202 | printf("%10d",p->line); 203 | printf("\n"); 204 | } 205 | } 206 | 207 | int marchID(char *word) 208 | { 209 | for (int i = 0; i < 32; i++) { 210 | if(strcmp(word, keyword[i]) == 0){ 211 | return i+1; 212 | } 213 | } 214 | return IDENTIFER; 215 | } 216 | 217 | void preProcess(char *word, int line) 218 | { 219 | const char *head_define = "define"; 220 | const char *head_include = "include"; 221 | char * p_include,*p_define; 222 | int flag = 0; 223 | p_include = strstr(word,head_include); 224 | if(p_include!=NULL) 225 | { 226 | flag = 1; 227 | int i; 228 | for(i=7;;) 229 | { 230 | if(*(p_include+i) == ' ' || *(p_include+i) == '\t') 231 | { 232 | i++; 233 | } 234 | else 235 | { 236 | break; 237 | } 238 | } 239 | createNewNode(p_include+i,HEADER_DESC,HEADER,-1,line); 240 | } 241 | else 242 | { 243 | p_define = strstr(word,head_define); 244 | if(p_define!=NULL) 245 | { 246 | flag = 1; 247 | int i; 248 | for(i=7;;) 249 | { 250 | if(*(p_define+i) == ' ' || *(p_define+i) == '\t') 251 | { 252 | i++; 253 | } 254 | else 255 | { 256 | break; 257 | } 258 | } 259 | createNewNode(p_define+i,CONSTANT_DESC,MACRO_VAL,-1,line); 260 | } 261 | } 262 | } 263 | 264 | 265 | int main(){ 266 | 267 | initNode(); 268 | FILE *fp; 269 | char ch; 270 | int count = 0; 271 | int line = 1; 272 | 273 | char tmp[30]; 274 | char *word; // 保存description 275 | 276 | if((fp = fopen("/users/xiaojia/desktop/options.txt", "r")) == NULL){ 277 | printf("文件不存在"); 278 | } 279 | 280 | while( ch!= EOF){ 281 | ch = fgetc(fp); 282 | //putchar(ch); 283 | 284 | 285 | //处理注释 286 | if (ch == '/') { 287 | ch = fgetc(fp); 288 | 289 | //处理 //类型// 290 | if(ch == '/') { 291 | while (ch != ' ' && ch != '\n') { 292 | ch = fgetc(fp); 293 | } 294 | createNewNode("空", NOTE_DESC, NOTE1, -1, line); 295 | } 296 | 297 | //处理 /*类型注释*/ 298 | else if(ch == '*'){ 299 | line++; 300 | ch = fgetc(fp); 301 | while (ch != '*'){ 302 | ch = fgetc(fp); 303 | if(ch == '\n'){ 304 | line++; 305 | } 306 | }while (ch != '/'){ 307 | ch = fgetc(fp); 308 | } 309 | createNewNode("空", NOTE_DESC, NOTE2, -1, line); 310 | } 311 | 312 | //处理 运算符/ 313 | else { 314 | createNewNode("/", OPERATOR_DESC, DIV, -1, line); 315 | } 316 | } 317 | 318 | //处理关键字和标识符 319 | if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'){ 320 | count = 0; 321 | while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' || (ch >= '0' && ch<= '9')) { 322 | tmp[count++] = ch; 323 | ch = fgetc(fp); 324 | } 325 | word = (char *)malloc(sizeof(char)*count); 326 | memcpy(word, tmp, count); 327 | word[count] = '\0'; 328 | int id = marchID(word); 329 | if (id == IDENTIFER ) { 330 | int address_id = createIdeNode(word, IDENTIFER_DESC, IDENTIFER, -1, line); 331 | createNewNode(word, IDENTIFER_DESC, IDENTIFER, address_id, line); 332 | }else{ 333 | createNewNode(word, KEY_DESC, id, -1, line); 334 | } 335 | fseek(fp,-1L,SEEK_CUR);//向后回退一位 336 | } 337 | 338 | 339 | //处理数字常量 340 | else if(ch >= '0' && ch <= '9'){ 341 | count = 0; 342 | int isFloat = 0; 343 | int isDouble = 0; 344 | while (ch >= '0' && ch <= '9') { 345 | tmp[count++] = ch; 346 | ch = fgetc(fp); 347 | } 348 | 349 | //处理float 350 | if (ch == '.') { 351 | isFloat = 1; 352 | tmp[count++] = ch; 353 | ch = fgetc(fp); 354 | if (ch >= '0' && ch <= '9') { 355 | while (ch >= '0' && ch <= '9' ){ 356 | tmp[count++] = ch; 357 | ch = fgetc(fp); 358 | } 359 | }else{ 360 | isFloat = -1; //出错,-1表示 小数点后没有数字. 361 | } 362 | } 363 | 364 | //处理double 365 | if(ch == 'E' || ch == 'e'){ 366 | isDouble = 1; 367 | tmp[count++] = ch; 368 | ch = fgetc(fp); 369 | if (ch == '+' || ch == '-') { 370 | tmp[count++] = ch; 371 | ch = fgetc(fp); 372 | } 373 | if (ch >= '0' && ch <= '9') { 374 | tmp[count++] = ch; 375 | ch = fgetc(fp); 376 | }else{ 377 | isDouble = -1; //出错,-1表示 E后面的格式不对. 378 | } 379 | } 380 | 381 | word = (char *)malloc(sizeof(char)*(count+1)); 382 | memcpy(word, tmp, count); 383 | word[count] = '\0'; 384 | if (isFloat==1) { 385 | createNewNode(word, CONSTANT_DESC, FLOAT_VAL, -1, line); 386 | }else if(isDouble==1){ 387 | createNewNode(word, CONSTANT_DESC, DOUBEL_VAL, -1, line); 388 | }else if(isFloat!=-1 && isDouble!=-1){ 389 | createNewNode(word, CONSTANT_DESC, INT_VAL, -1, line); 390 | } 391 | fseek(fp,-1L,SEEK_CUR);//向后回退一位 392 | } 393 | 394 | //处理换行 395 | else if(ch == ' ' || ch == '\t' || ch == '\r' || ch =='\n' ){ 396 | if(ch == '\n'){ 397 | line++; 398 | } 399 | } 400 | 401 | //处理头文件和宏常量(预处理) 402 | else if(ch == '#'){ 403 | count = 0; 404 | while(ch!='\n' && ch!=EOF) 405 | { 406 | tmp[count++] = ch; 407 | ch = fgetc(fp); 408 | } 409 | word = (char *)malloc(sizeof(char)*(count+1)); 410 | memcpy(word,tmp,count); 411 | word[count] = '\0'; 412 | preProcess(word,line); 413 | fseek(fp,-1L,SEEK_CUR); 414 | } 415 | 416 | else if(ch == '*'){ 417 | createNewNode("*", OPERATOR_DESC, MUL, -1, line); 418 | } 419 | 420 | else if(ch == '>'){ 421 | createNewNode(">", OPERATOR_DESC, GTEATER_THAN, -1, line); 422 | } 423 | 424 | else if(ch == '>'){ 425 | createNewNode("<", OPERATOR_DESC, LEFT_PAR, -1, line); 426 | } 427 | 428 | else if(ch == ';'){ 429 | createNewNode(";", BOUNTWROD_DESC, SEM, -1, line); 430 | } 431 | 432 | else if(ch == ','){ 433 | createNewNode(",", BOUNTWROD_DESC, COM, -1, line); 434 | } 435 | 436 | else if(ch == '"'){ 437 | createNewNode("\"", BOUNTWROD_DESC, DOUBLE_QUO, -1, line); 438 | } 439 | 440 | else if(ch == ':'){ 441 | createNewNode(":", BOUNTWROD_DESC, COL, -1, line); 442 | } 443 | 444 | else if(ch == '('){ 445 | createNewNode("(", BOUNTWROD_DESC, LEFT_PAR, -1, line); 446 | } 447 | 448 | else if(ch == ')'){ 449 | createNewNode(")", BOUNTWROD_DESC, RIGHT_PAR, -1, line); 450 | } 451 | 452 | 453 | //{"=",";",",","'",":","(",")","."}; 454 | 455 | 456 | if (ch != ' '&& ch != '\n') { //去掉空格和换行符 457 | charSET[count] = ch; 458 | count++; 459 | } 460 | } 461 | fclose(fp); 462 | 463 | displaynormalNode(); 464 | 465 | } -------------------------------------------------------------------------------- /Compiler/options.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 6 7 8 2 | 7 8 6 5 7 9 7 8 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Compiler 2 | ======== 3 | 一个简易的C语言编译器(分析C语言)。 4 | 5 | 实现词法分析部分。 6 | -------------------------------------------------------------------------------- /options.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 6 7 8 2 | 7 8 6 5 7 9 7 8 --------------------------------------------------------------------------------