c++ REGEX 判断是float
时间: 2023-11-26 20:03:03 浏览: 45
同样,我们可以使用正则表达式来判断一个字符串是否为浮点数。以下是一个示例代码:
```cpp
#include <iostream>
#include <regex>
int main() {
std::string str = "1.23";
std::regex reg("-?[0-9]+(.[0-9]+)?");
bool is_float = std::regex_match(str, reg);
std::cout << is_float << std::endl; // 输出1,表示是浮点数
return 0;
}
```
在上面的代码中,我们使用了一个正则表达式 `-?[0-9]+(.[0-9]+)?` 来判断一个字符串是否为浮点数。其中,`-?` 表示可选的负号,`[0-9]+` 表示一个或多个数字,`(.[0-9]+)?` 表示可选的小数部分,其中 `.` 表示小数点,`[0-9]+` 表示小数部分的数字。同样,`std::regex_match` 函数用于判断字符串是否与正则表达式匹配。如果匹配成功,返回值为 `true`,否则返回 `false`。
相关问题
C++ TINY扩充正则表达式的语法
根据提供的引用内容,C++ TINY扩充正则表达式的语法如下:
1. program -> declarations stmt-sequence
2. declarations -> decl ; declarations | ε
3. decl -> type-specifier var-list
4. type-specifier -> int | bool | string | float | double | regex
5. var-list -> identifier { , identifier }
6. statement -> if-stmt | repeat-stmt | assign-stmt | read-stmt | write-stmt | while-stmt
7. while-stmt -> do stmt-sequence while bool-exp
8. bool-exp -> bool-term { or bool-term }
9. bool-term -> bool-factor { and bool-factor }
10. bool-factor -> [not] bool-primary
11. bool-primary -> identifier | literal | ( bool-exp ) | regex-exp
12. regex-exp -> / pattern /
13. pattern -> pattern-primary { pattern-concat pattern-primary }
14. pattern-primary -> character | . | ( pattern )
15. pattern-concat -> pattern-primary { pattern-primary }
16. character -> a | b | ... | z | A | B | ... | Z | 0 | 1 | ... | 9
其中,regex表示正则表达式类型,regex-exp表示正则表达式表达式,pattern表示正则表达式模式,pattern-primary表示正则表达式模式的基本单元,pattern-concat表示正则表达式模式的连接操作。
用python 模拟c++编译器的程序
模拟 C 编译器的程序需要使用 Python 语言创建一个解析器,将 C 代码转化为机器码。这个过程大致可以分为三个步骤:
1. 词法分析:将原始的 C 代码分解为关键字、标识符、运算符等基本单元,这个过程叫做词法分析。
2. 语法分析:将分解后的代码按照语法规则组织成语法树,这个过程叫做语法分析。
3. 代码生成:将语法树转换为机器码,并输出可执行程序。
下面是一个简单的示例,演示如何使用 Python 实现一个简单的 C 编译器:
```python
import re
# 定义 C 语言的关键字
keywords = ['auto', 'break', 'case', 'char', 'const', 'continue', 'default', 'do', 'double', 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if', 'int', 'long', 'register', 'return', 'short', 'signed', 'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while']
# 定义运算符和分隔符
operators = ['+', '-', '*', '/', '=', '>', '<', '>=', '<=', '==', '!=', '&&', '||', '!', '&', '|', '^', '~', '<<', '>>', '++', '--']
delimiters = ['(', ')', '{', '}', ';', ',']
# 定义一个函数,将源代码分解为基本单元
def tokenize(code):
tokens = []
source_code = code.strip()
while len(source_code) > 0:
match = None
# 匹配关键字、标识符、数字和字符串
for keyword in keywords:
pattern = '^' + keyword + r'(?![a-zA-Z0-9_])'
regex = re.compile(pattern)
match = regex.match(source_code)
if match:
tokens.append(('keyword', keyword))
source_code = source_code[match.end():].strip()
break
if not match:
pattern = r'^([a-zA-Z_]\w*)'
regex = re.compile(pattern)
match = regex.match(source_code)
if match:
tokens.append(('identifier', match.group()))
source_code = source_code[match.end():].strip()
continue
pattern = r'^(\d+)'
regex = re.compile(pattern)
match = regex.match(source_code)
if match:
tokens.append(('number', match.group()))
source_code = source_code[match.end():].strip()
continue
pattern = r'^"([^"]*)"'
regex = re.compile(pattern)
match = regex.match(source_code)
if match:
tokens.append(('string', match.group(1)))
source_code = source_code[match.end():].strip()
continue
# 匹配运算符和分隔符
for operator in operators + delimiters:
pattern = '^' + re.escape(operator)
regex = re.compile(pattern)
match = regex.match(source_code)
if match:
tokens.append(('operator', operator))
source_code = source_code[match.end():].strip()
break
if not match:
raise SyntaxError("Invalid syntax: " + source_code)
return tokens
# 将基本单元按照语法规则组织成语法树
def parse(tokens):
pass
# 将语法树转换为机器码,并输出可执行程序
def generate_code(tree):
pass
# 主程序,从文件中读取 C 代码,并编译为可执行程序
if __name__ == '__main__':
with open('test.c', 'r') as f:
code = f.read()
tokens = tokenize(code)
tree = parse(tokens)
generate_code(tree)
```
这个示例只实现了第一个步骤,将 C 代码分解为基本单元。你可以根据需要,自行实现后续的语法分析和代码生成过程。