jyami
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # リダイレクションの構文 - シェルもどきをgoで自作する #7 ## おさらい これまで - [シェルってなに?コマンドラインインタプリタってなに? - シェルもどきをgoで自作する#1](https://hackmd.io/@jyami/HJzohRn2D) - [コマンドと引数の分解、環境変数PATHから探索、外部コマンドと内部コマンド - シェルもどきをgoで自作する #2](https://hackmd.io/@jyami/HyeSkkThP) - [字句解析その1 - シェルもどきをgoで自作する #3](https://hackmd.io/@jyami/Hk3bWSMQO) - [字句解析その2 - シェルもどきをgoで自作する #4](https://hackmd.io/@jyami/S1BkltxQu) - [リダイレクションってなに? - シェルもどきをgoで自作する #5](https://hackmd.io/@jyami/S113NQx8u) - [リダイレクションの種類 - シェルもどきをgoで自作する #6](https://hackmd.io/@jyami/rJ3XmClqd) シェルもどき「[oreshell](https://github.com/jyami/oreshell)」を自作している。 現状のoreshellはリダイレクションを行うとエラーになるのでリダイレクションできるように修正する。 前回はリダイレクションをどこまで対応するかを決めた。 今回は対応するリダイレクションの「構文」を決める。 ## え、構文ってなに? > 構文は、正しく構造化されたコードを構成する文字の必要な組み合わせと順序を指定します。 > [MDN Web Docs用語集](https://developer.mozilla.org/ja/docs/Glossary/Syntax) 構文 = シンタックス > シンタックスとは、統語論、構文(論)という意味の英単語で、ITの分野ではプログラミング言語などの人工言語の仕様として定められた文法や表記法、構文規則などのルールを指す。 > 処理系が正しいコードとして解釈可能な文字や記号の並べ方、単語や文の組み合わせ方などの規則の総体である。 > プログラミング言語の場合、例えば「複数の文を並べる場合はセミコロンで区切る」「変数は使用前に型と変数名を宣言する」などがシンタックスにあたる。 > [IT用語辞典](https://e-words.jp/w/%E3%82%B7%E3%83%B3%E3%82%BF%E3%83%83%E3%82%AF%E3%82%B9.html) 今回の場合は、シェル(oreshell)が解釈可能な単語や文の組み合わせ方の規則を表す。 (厳密には解釈可能な文字や記号の並べ方も定義する必要があるが今回は記述をサボっている。) ## 構文定義の表現方法 単語や文の組み合わせ方を日本語でダラダラと書くと曖昧さを生むことがあるので、今回はBNF/EBNFを使って定義する。 ### BNF、EBNF > バッカス・ナウア記法(英: Backus–Naur form)とは、文脈自由文法を定義するのに用いられるメタ言語のことで、一般にBNFやBN記法と略される。 > 現在はこのBNFを拡張したEBNF (Extended BNF) が一般的に使われている。 > EBNFでは正規表現を用いてより簡単に記述でき、プロトコル規定言語であるASN.1や、XMLの構文定義にも利用されている。 > パーサジェネレータを使用して構文解析器を生成する際に、構文を定義するためにも使う。 > [wikipedia](https://ja.wikipedia.org/wiki/%E3%83%90%E3%83%83%E3%82%AB%E3%82%B9%E3%83%BB%E3%83%8A%E3%82%A6%E3%82%A2%E8%A8%98%E6%B3%95) ソフトウェア開発で構文を定義するときは、BNFやEBNFやそれ以外のBNF拡張を使う事が多い。 (余談:[ANTLR](https://ja.wikipedia.org/wiki/ANTLR)も文法の記述方法は、EBNFに似た形式となっている。) ### railroad diagram railroad diagram (syntax diagram) > Syntax diagrams (or railroad diagrams) are a way to represent a context-free grammar. They represent a graphical alternative to Backus–Naur form, EBNF, Augmented Backus–Naur form, and other text-based grammars as metalanguages. > BNF is text-based, and used by compiler writers and parser generators. Railroad diagrams are visual, and may be more readily understood by laypeople, sometimes incorporated into graphic design. > 構文図 (または構文図) は、文脈自由文法を表す方法です。これらは、メタ言語として、Backus–Naur形式、EBNF、Augmented Backus–Naur形式、およびその他のテキストベースの文法のグラフィカルな代替を表します。 > BNFはテキストベースであり、コンパイラライタやパーサジェネレータで使用されます。鉄道図面は視覚的なものであり、素人でも容易に理解することができ、時にはグラフィックデザインに組み込まれる。 > [wikipedia](https://en.wikipedia.org/wiki/Syntax_diagram) > [みらい翻訳](https://miraitranslate.com/trial) railroad diagramはBNF/EBNFの記述を鉄道の線路に似せた図で表現したもの。 以下のサイトでEBNFからrailroad diagramを作成することができる。 [Railroad Diagram Generator](https://www.bottlecaps.de/rr/ui) ## リダイレクション対応前の構文 リダイレクション対応前は、コマンドの後に続けてコマンド引数群のみ指定できる。 ざっくり日本語で説明すると以下の通り。 ``` コマンド名 引数1 引数2 ... 引数n ``` これを以降でBNFとEBNFとrailroad diagramで表す。 (同じ内容をBNFやEBNFで記述する方法はこれ以外にも幾通りもあるが今回のoreshellリダイレクション対応はこの方法で表す。) ### コマンド部分の構文 BNF: ``` simple_command :: cmd_name | cmd_name word_list ``` EBNF: ``` simple_command ::= cmd_name word_list? ``` railroad diagram: ![](https://i.imgur.com/EYleREO.png) 「コマンド引数群(word_list)を指定することもできるし、指定しないこともできる」ということを表している。 railroad diagramの説明。 左末端が始発駅、右末端が終着駅を表している。 分岐した線路を描くことでが「選択」を表している。 あわせて、BNFとEBNFにおける「選択」の表現方法の違いについてもよく見てみるとおもしろい。 ### コマンド引数群部分(word_list)の構文 BNF: ``` word_list :: word | word_list word ``` EBNF: ``` word_list ::= word+ ``` railroad diagram: ![](https://i.imgur.com/UL5lFYn.png) 「コマンド引数(word)は複数指定できる」ということを表している。 railroad diagramの説明。 ぐるぐるとループできるよう線路に描くことで「繰り返し(複数)」を表している。 あわせて、BNFとEBNFにおける「繰り返し」の表現方法の違いについてもよく見てみるとおもしろい。 ### 全体の構文 全部くっつけると以下の通り。(BNFは省略) EBNF: ``` simple_command_all ::= cmd_name (word+)? ``` railroad diagram: ![](https://i.imgur.com/2BFxzR0.png) ## リダイレクション対応後の構文 リダイレクション対応後は、コマンドの後に続けてコマンド引数群とリダイレクション群を指定できる。 ざっくり日本語で説明すると以下の通り。 ``` コマンド名 引数1 引数2 ... 引数n <リダイレクション1> <リダイレクション2> ... <リダイレクションn> ``` これを以降でBNFとEBNFとrailroad diagramで表す。 ### コマンド部分の構文 BNF: ``` simple_command :: cmd_name | cmd_name cmd_suffix ``` EBNF: ``` simple_command ::= cmd_name cmd_suffix? ``` railroad diagram: ![](https://i.imgur.com/i7JGDG5.png) 「コマンド引数/リダイレクション群(cmd_suffix)を指定することもできるし、指定しないこともできる」ということを表している。 ### コマンド引数/リダイレクション群部分(cmd_suffix)の構文 BNF: ``` cmd_suffix :: io_redirect | cmd_suffix io_redirect | WORD | cmd_suffix WORD ``` EBNF: ``` cmd_suffix ::= (word | io_redirect)+ ``` ![](https://i.imgur.com/sENq7Of.png) 「コマンド引数(WORD)とリダイレクション(io_redirect)は複数指定できる」ということを表している。 ### リダイレクション部分(io_redirect)の構文 BNF: ``` io_redirect :: io_file | IO_NUMBER io_file ``` EBNF: ``` io_redirect ::= IO_NUMBER? io_file ``` railroad diagram: ![](https://i.imgur.com/xzpAnej.png) IO_NUMBERはファイルディスクリプタ番号を表す。 「ファイルディスクリプタ番号を指定することもできるし、指定しないこともできる」ということを表している。 ### リダイレクション方向とファイル名部分(io_file)の構文 BNF: ``` io_file :: '<' filename | '>' filename ``` EBNF: ``` io_file ::= ('<' | '>') filename ``` railroad diagram: ![](https://i.imgur.com/1nZzmUU.png) 「リダイレクション方向として「>」または「<」を指定でき、そのあとファイル名を指定する」ということを表している。 ### 全体の構文 全部くっつけると以下の通り。(BNFは省略) EBNF: ``` simple_command ::= cmd_name ((word | (IO_NUMBER? ('<' | '>') filename))+)? ``` railroad diagram: ![](https://i.imgur.com/XutVWfu.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully