この記事には複数の問題があります。改善
やノートページでの議論にご協力ください。字句(じく)とは、プログラミング言語などにおいてソースコードに出現する文字列の中で意味を持つ最小単位であり、トークン(英: token)とも言う。
詳細は言語によって異なるが、一般的には、以下のようなトークンがある。
ifやintなどのキーワード(あるいは予約語)
main や argc などといった関数(サブルーチン)名や変数名などの識別子
{ } ( ) [ ] などの括弧類(の片方ずつ)
+ - * / = > < & | などの演算子[注釈 1]
, . : ; などの区切り文字
3.14159や"hello" のようなリテラル
括弧類や区切り文字が出現位置によっては演算子となる言語もある。
多くの場合、プログラミング言語の構文規則はトークン列を定義しており[2]、構文解析の前に、まずトークンを切り出し、必要であれば種類別に分類したりもする。この処理を字句解析と呼ぶ。この処理のため、プログラミング言語の言語仕様では、それぞれの種類のトークンはどのような文字から構成されるかが規定されている。
(例)識別子は、英字で始まり、その後に0個以上の英字、数字または下線 (_) が続く。ただしキーワード(あるいは予約語)と同じ綴りでないこと。
一般に構文規則はその文脈自由文法が(バッカス・ナウア記法で)示されることが多いのに対し、字句規則は正規表現で示されることが多い。上の例を正規表現で表すと [A-Za-z][0-9A-Z_a-z]* となる。ここで、「キーワード(あるいは予約語)と同じ綴りでないこと」という制限は、この表現には含まれない。プログラミング言語における規則には、一般にそういった制限が多いが、それを正規表現で表現するのは難しい(長大になる)ことも多い。キーワードの規則と識別子の規則はよく似たものであることも多く、いったんキーワードと識別子を同じ規則で処理しておき、その後に出現位置などの文脈に応じて分類し直すほうが効率的でもある。拡張性や後方互換性の観点から、通常は識別子として利用することもできるが、特定の出現位置ではキーワードの役割を果たすような「文脈依存キーワード」(contextual keyword, keyword in context, context-sensitive keyword) をサポートする言語も多い[3][4]。
脚注
注釈^ ++, --, **, //, ==, >>, <<, &&, ||, ->, => のように連続する2文字でひとつのトークンを形成する場合もある。divやmod、andやorのように、記号だけでなく自然言語の単語に似た英数字の文字列から成る演算子を持つ言語もある。プログラマが独自の文字シーケンスから成る演算子をカスタム定義できる言語もある[1]。
出典^ シンボルと演算子のリファレンス - F# 。Microsoft Learn
^ C Tokens 。Microsoft Learn
^ C# Keywords 。Microsoft Learn
^ Context-Sensitive Keywords (C++/CLI and C++/CX) 。Microsoft Learn
関連項目
字句解析
構文解析
.mw-parser-output .asbox{position:relative;overflow:hidden}.mw-parser-output .asbox table{background:transparent}.mw-parser-output .asbox p{margin:0}.mw-parser-output .asbox p+p{margin-top:0.25em}.mw-parser-output .asbox{font-size:90%}.mw-parser-output .asbox-note{font-size:90%}.mw-parser-output .asbox .navbar{position:absolute;top:-0.90em;right:1em;display:none}
この項目は、コンピュータに関連した書きかけの項目です。この項目を加筆・訂正などしてくださる協力者を求めています(PJ:コンピュータ/P:コンピュータ)。
.mw-parser-output .hlist ul,.mw-parser-output .hlist ol{padding-left:0}.mw-parser-output .hlist li,.mw-parser-output .hlist dd,.mw-parser-output .hlist dt{margin-right:0;display:inline-block;white-space:nowrap}.mw-parser-output .hlist dt:after,.mw-parser-output .hlist dd:after,.mw-parser-output .hlist li:after{white-space:normal}.mw-parser-output .hlist li:after,.mw-parser-output .hlist dd:after{content:" ・\a0 ";font-weight:bold}.mw-parser-output .hlist dt:after{content:": "}.mw-parser-output .hlist-pipe dd:after,.mw-parser-output .hlist-pipe li:after{content:" |\a0 ";font-weight:normal}.mw-parser-output .hlist-hyphen dd:after,.mw-parser-output .hlist-hyphen li:after{content:" -\a0 ";font-weight:normal}.mw-parser-output .hlist-comma dd:after,.mw-parser-output .hlist-comma li:after{content:"、";font-weight:normal}.mw-parser-output .hlist-slash dd:after,.mw-parser-output .hlist-slash li:after{content:" /\a0 ";font-weight:normal}.mw-parser-output .hlist dd:last-child:after,.mw-parser-output .hlist dt:last-child:after,.mw-parser-output .hlist li:last-child:after{content:none}.mw-parser-output .hlist dd dd:first-child:before,.mw-parser-output .hlist dd dt:first-child:before,.mw-parser-output .hlist dd li:first-child:before,.mw-parser-output .hlist dt dd:first-child:before,.mw-parser-output .hlist dt dt:first-child:before,.mw-parser-output .hlist dt li:first-child:before,.mw-parser-output .hlist li dd:first-child:before,.mw-parser-output .hlist li dt:first-child:before,.mw-parser-output .hlist li li:first-child:before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child:after,.mw-parser-output .hlist dd dt:last-child:after,.mw-parser-output .hlist dd li:last-child:after,.mw-parser-output .hlist dt dd:last-child:after,.mw-parser-output .hlist dt dt:last-child:after,.mw-parser-output .hlist dt li:last-child:after,.mw-parser-output .hlist li dd:last-child:after,.mw-parser-output .hlist li dt:last-child:after,.mw-parser-output .hlist li li:last-child:after{content:")\a0 ";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li:before{content:" "counter(listitem)" ";white-space:nowrap}.mw-parser-output .hlist dd ol>li:first-child:before,.mw-parser-output .hlist dt ol>li:first-child:before,.mw-parser-output .hlist li ol>li:first-child:before{content:" ("counter(listitem)" "}.mw-parser-output .navbar{display:inline;font-size:75%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}.mw-parser-output .infobox .navbar{font-size:88%}.mw-parser-output .navbox .navbar{display:block;font-size:88%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}